Newbie question

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
 
N

Nathaniel Talbott

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

<:((><
 
W

Wesley J Landaker

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

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 <[email protected]>
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--
 
J

Jim Weirich

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

Gavin Sinclair

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
 
G

Gavin Sinclair

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

Bill Kelly

Hi,

From: "Stephen Taylor said:
[...]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top