形状继承示例和“Ruby 方式”
在我从十年的 C++ 过渡到 Ruby 的过程中,我发现自己在重新思考如何完成最简单的事情。 鉴于下面的经典形状推导示例,我想知道这是否是“Ruby Way”。 虽然我相信下面的代码本质上没有任何问题,但我感觉我没有充分利用 Ruby 的全部功能。
class Shape
def initialize
end
end
class TwoD < Shape
def initialize
super()
end
def area
return 0.0
end
end
class Square < TwoD
def initialize( side )
super()
@side = side
end
def area
return @side * @side
end
end
def shapeArea( twod )
puts twod.area
end
square = Square.new( 2 )
shapeArea( square ) # => 4
这是实施“Ruby Way”吗? 如果没有,您将如何实施?
In my quest to transition from a decade of C++ to Ruby, I find myself second guessing how to accomplish the simplest things. Given the classic shape derivation example below, I'm wondering if this is "The Ruby Way". While I believe there's nothing inherently wrong with the code below, I'm left feeling that I'm not harnessing the full power of Ruby.
class Shape
def initialize
end
end
class TwoD < Shape
def initialize
super()
end
def area
return 0.0
end
end
class Square < TwoD
def initialize( side )
super()
@side = side
end
def area
return @side * @side
end
end
def shapeArea( twod )
puts twod.area
end
square = Square.new( 2 )
shapeArea( square ) # => 4
Is this implemented "The Ruby Way"? If not, how would you have implemented this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ruby 的美妙之处在于,您不必仅仅为了提供已实现方法的契约而使用继承。 相反,您可以这样做:
这是一个称为鸭子类型的功能。 只要twod有一个area方法(用Ruby的说法,我们会说twod响应area),你就可以开始了。
The beautiful thing about Ruby is you don't have to use inheritance just to provide a contract of implemented methods. Instead, you could have done this:
This is a feature known as duck typing. So long as twod has an area method (in Ruby parlance we'd say twod responds to area), you're good to go.
第一个答案是“Ruby 方式”,但如果您感受到从 C++(高度类型)到 ruby(鸭子类型)的变化,您可以让它感觉更像家:
当然,您正在过渡到 ruby,所以你也可以避免重复自己:
The first answer is "the ruby way", but if you're feeling the change from C++ (highly-typed) to ruby (duck-typing) you can make it feel more like home:
Of course you're transitioning to ruby so you could also avoid repeating yourself: