在红宝石中使用组合(Using Composition in ruby)

编程入门 行业动态 更新时间:2024-10-27 07:21:15
在红宝石中使用组合(Using Composition in ruby)

我是新的Ruby,但一年来一直是.net开发者。 我希望在我的几个模型中实现组合,以确保它们尽可能松散耦合,但不知道从哪里开始,或者如果真的需要它,我仍然想要像.net开发。

任何人都可以给我一些指示从哪里开始。

干杯科林G

I'm new Ruby but been a .net dev for many a year. I want to implement composition within a couple of my model to make sure they are as loosely coupled as possible but have no idea where to start, or if this is really needed and I'm still thinking to much like a .net dev.

Can anyone give me some pointers on where to start.

Cheers Colin G

最满意答案

Ruby是一种面向对象但动态类型的语言。 作为一种动态语言,Ruby开发人员倾向于使用代码的反射和动态修改。 当然因为它是一种面向对象的语言,你可以使用与.net中相同的原则,你也应该这样做,但总是环顾四周,看看如何以更动态的方式实现同​​样的事情。

例如,ActiveRecord ORM使用方法 compos_of解决了组合问题,该方法将动态地为您的类添加适当的字段和属性。 我并不是说这就是它应该完成的方式(例如DataMapper,它是另一个Ruby的ORM,选择了一种更“保守”的方法,所以更类似于(Fluent)NHibernate),这只是一个例子如何以不同的方式做事。

像AOP或DI这样的东西不是动态语言的外国概念,它们通常是以另一种方式完成的。 对语言的动态方面保持开放的态度,但不要过头。

Ruby is an object-oriented but dynamically typed language. Being a dynamic language, rubyists tend to use reflections and dynamic modifying of the code much more than .net developers. Of course because it's an object-oriented language you can use mostly the same principles as in .net, and you should too, but always look around and see how that same thing could be implemented in a more dynamic way.

For example the ActiveRecord ORM solves composition using a composed_of method that will dynamically add the appropriate fields and properties to your class. I'm not saying that this is the way it should be done (for example DataMapper, which is another ORM for ruby, choose a more "conservative" approach, and so resembles more like (Fluent)NHibernate), it's just an example of how things can be done differently.

Things like AOP, or DI aren't a foreign concept for dynamic languages, they are just usually done in an alternative way. Keep an open mind on the dynamic aspects of the language but don't overdo them.

更多推荐

本文发布于:2023-07-08 01:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   红宝石   ruby   Composition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!