# Newbie question

Discussion in 'Ruby' started by Stephen Taylor, Feb 3, 2004.

1. ### Stephen TaylorGuest

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"

Stephen Taylor & Ray Cannon

Stephen Taylor, Feb 3, 2004

2. ### Nathaniel TalbottGuest

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)

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

<(><

Nathaniel Talbott, Feb 3, 2004

3. ### Wesley J LandakerGuest

--Boundary-02=_5aBIA7kcgqDHV0X
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline

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
> '=A3' + @value.to_s
> end
> end
>
> permits:
>
> m =3D Money.new
> m.value=3D 1234
> puts m.to_s
> "=A31234"
>
> Question: can we create and set in one move? We imagine
>
> n =3D Money.new(1234)
>
> But if we:
>
> def Method.new(amt)
> initialize
> @value =3D amt
> end
>
> we get:
>
> n =3D Money.new(1234)
> n.to_s
> "1234"
> n.class
> "Fixnum"
>

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

class Money
def initialize(amt)
@value =3D amt.to_i
end
def to_s
' ' + @value.to_s
end
end

m =3D Money.new(1234)
m.to_s=20
m.class=20

=2D-=20
Wesley J. Landaker <>
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2

--Boundary-02=_5aBIA7kcgqDHV0X
Content-Type: application/pgp-signature
Content-Description: signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBAIBa58KmKTEzW49IRAi3KAJ4jXfpaSCdwGw1ATPiw/fvn1a2dDQCdGzf3
fnds3XbA33NQLZxnF2+4opw=
=uqE9
-----END PGP SIGNATURE-----

--Boundary-02=_5aBIA7kcgqDHV0X--

Wesley J Landaker, Feb 3, 2004
4. ### Jim WeirichGuest

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

>
> 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 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)

Jim Weirich, Feb 3, 2004
5. ### Gavin SinclairGuest

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
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

Gavin Sinclair, Feb 3, 2004
6. ### Gavin SinclairGuest

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

^^^^^^^^^ er, Money#initialize...

Gavin Sinclair, Feb 3, 2004
7. ### Bill KellyGuest

Hi,

From: "Stephen Taylor" <>
>

[...]
>
> 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

Bill Kelly, Feb 9, 2004