S
Stephen Taylor
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)
But if we:
def Method.new(amt)
initialize
@value = amt
end
we get:
n = Money.new(1234)
n.to_s
"1234"
n.class
"Fixnum"
Advice?
Stephen Taylor & Ray Cannon
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)
But if we:
def Method.new(amt)
initialize
@value = amt
end
we get:
n = Money.new(1234)
n.to_s
"1234"
n.class
"Fixnum"
Advice?
Stephen Taylor & Ray Cannon