I’m wondering if one could define a class in Ruby to have the following sort of usage:
Class Book
def Book
puts self.to_s
end
def initialize(name)
@name = name
end
def to_s
@name.to_s
end
end
Usage:
Book "To Kill a Mocking Bird" #=>To Kill a Mocking Bird
The idea that I want is for this to behave like the following
- An instance of the method is created (as a short hand).
- The method Book is immediately called after this and performs a block of code.
(The intent of having the method named the same as the class is to have the call back when it is used like a method.)
Is this possible in Ruby?
How about this?
As a minor point of interest, Ruby’s
Kernelmodule uses such a technique (written in C) to implement methods namedArray,String, and so on: