Newbie question


(Stephen Taylor) #1

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


(Nathaniel Talbott) #2

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

<:((><

···

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)

(Wesley J Landaker) #3

You don’t want to override the ‘new’ method, you want to override the
’initialize’ method in your class. Something like this:

class Money
def initialize(amt)
@value = amt.to_i
end
def to_s
’ ’ + @value.to_s
end
end

m = Money.new(1234)
m.to_s
m.class

···

On Tuesday 03 February 2004 2:35 pm, 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)

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?


Wesley J. Landaker wjl@icecavern.net
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2


(Jim Weirich) #4

Stephen Taylor said:

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.

Perhaps another grey-haired programmer can answer :slight_smile:

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.)
[… code elided …]

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

Remember that new is a class method, therefore references to @value inside
of new will reference the value attribute of the class object Money, not
an instance of Money.

Fortunately, the built-in version of new calls initialize after the object
is allocated. Initialize is an instance method, so you can do what you
need in it.

For example

class Money
def initialize(value)
@value = value
end
# … rest of class here …
end

n = Money.new(1234) # Should now work.

···


– Jim Weirich jim@weirichhouse.org http://onestepback.org

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)


(Gavin Sinclair) #5

On Wednesday, February 4, 2004, 8:35:05 AM, Stephen wrote, in part:

But if we:

def Method.new(amt)
    initialize
    @value = amt
end

There’s the problem. Try this:

class Money
attr_reader :value
def initialize(amount)
@value = amount
end
def to_s
’£’ + @value.to_s
end
end

m = Money.new(1234)
m.to_s

Untested…

What you’ve missed is that Money.new is already defined (Class#new),
and it will call Money#initialize, which is all you have to define.

Class#new creates a new instance of any class
Money#new initializes an instance of Money

Cheers,
Gavin


(Bill Kelly) #6

Hi,

[…]

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

Is Method.new a typo for Money.new?

But instead, try defining an initialize method, rather than new, like:

class Money
def initialize(amt)
@value = amt
end
end

Then: n = Money.new(1234)

should do what you wanted…

Hope this helps,

Bill

···

From: “Stephen Taylor” sjt@5jt.com


(Gavin Sinclair) #7

^^^^^^^^^ er, Money#initialize…

···

On Wednesday, February 4, 2004, 8:54:48 AM, Gavin wrote:

What you’ve missed is that Money.new is already defined (Class#new),
and it will call Money#initialize, which is all you have to define.

Class#new creates a new instance of any class
Money#new initializes an instance of Money