开发者

Ruby syntax question: Rational(a, b) and Rational.new!(a, b)

Today I came across the strange ruby synt开发者_如何学Cax in the Rational class:

Rational(a,b)

(Notice the absence of the .new()portion compared to the normal Ruby syntax). What does this mean, precisely, compared to the normal new syntax? More importantly, how do I implement something like this in my own code, and why would I implement something like this? Specifically for the Rational class, why is this syntax used instead of the normal instantiation? And why is the new method private in the rational class? (And how/why would I do this in my own ruby code?) Thanks in advance for your answers, especially since I've asked so many questions.


All you have to do is declare a global function with the same name as your class. And that is what rational.rb does:

def Rational(a, b = 1)
  if a.kind_of?(Rational) && b == 1
    a
  else
    Rational.reduce(a, b)
  end
end

to make the constructor private:

private :initialize

and similarly for the new method:

private_class_method :new

I suppose Rational.new could be kept public and made to do what Rational() does, but having a method that turns its arguments into instances is consistent with Array(), String(), etc. It's a familiar pattern that's easy to implement and understand.


The method Rational() is actually an instance method defined outside of the class Rational. It therefore becomes an instance method of whatever object loads the library 'rational' (normally main:Object) in the same way that 'puts' does, for example.

By convention this method is normally a constructor for the class of the same name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜