Velocity Reviews > Ruby > Newbie question

# Newbie question

Stephen Taylor
Guest
Posts: n/a

 02-03-2004
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
Guest
Posts: n/a

 02-03-2004
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

<(><

Wesley J Landaker
Guest
Posts: n/a

 02-03-2004
--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"
>
> Advice?

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 <(E-Mail Removed)>
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--

Jim Weirich
Guest
Posts: n/a

 02-03-2004

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://www.velocityreviews.com/forums/(E-Mail Removed) 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
Guest
Posts: n/a

 02-03-2004
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

Gavin Sinclair
Guest
Posts: n/a

 02-03-2004
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...

Bill Kelly
Guest
Posts: n/a

 02-09-2004
Hi,

From: "Stephen Taylor" <(E-Mail Removed)>
>

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

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM Lee UK VOIP 4 05-17-2005 04:10 PM No Spam Cisco 3 06-07-2004 10:02 AM Jerry C. Perl Misc 8 11-23-2003 04:11 AM Id0x Python 4 07-20-2003 11:40 PM

Advertisments