On Feb 3, 2004, at 16:35, Stephen Taylor wrote:

> We are two grey-haired programmers studying Ruby and have a question so
> basic we can't believe we 're unable to find an answer in "The Ruby 
> Way" and
> "Ruby in 21 Days" or the online material.
>
> Our class Money holds pennies as Fixnum values and its to_s method 
> returns a
> string with the currency symbol prefixed. (Leave commas and decimal 
> point
> for later.)
>
>     class Money
>     attr_accessor :value
>         def to_s
>             '?' + @value.to_s
>         end
>     end
>
> permits:
>
>     m = Money.new
>     m.value= 1234
>     puts m.to_s
>     "?1234"
>
> Question: can we create and set in one move? We imagine
>
>     n = Money.new(1234)

How about:

   class Money
     def initialize
       @value = 1234
     end
   end

Ruby's default version of the Class#new method calls initialize for 
you, so you just have to override it in your class.

HTH,


Nathaniel

<:((><