[NEWBIE]: Example from Pickaxe2 gives errors... (p. 31)

G

Guest

Greeting all,

How about a refreshingly easy question for a change....


Really, really basic question I know, but:

class Song
attr_writer :name, :artist, :duration
end
=> nil

song = Song.new("Blueberry Hill", "Fats Waller", 320)
=> ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize'
from (irb):24:in `new'
from (irb):24

However....

song = Song.new()
=> #<Song:0x2aab648>

song.name = "Blueberry Hill"
=> "Blueberry Hill"

song.name
=> "Blueberry Hill"

Have I misread / misunderstood the text? Why the errors?

SM

(Only 770 pages to go... Woohoo!)

------------------------------------------------------------------------------------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsolutions.com
Equinox Converged Solutions is a trading name of Synetrix Holdings Limited.

IMPORTANT NOTICE:
This message is intended solely for the use of the Individual or organisation to whom it is addressed. It may contain privileged or confidential information. If you have received this message in error, please notify the originator immediately.
If you are not the intended recipient, you should not use, copy, alter, or disclose the contents of this message. All information or opinions expressed in this message and/or any attachments are those of the author and are not necessarily those of Synetrix Holdings Limited.
Synetrix Holdings Limited accepts no responsibility for loss or damage arising from its use, including damage from virus.
-------------------------------------------------------------------------------------------
 
B

Bill Atkins

------=_Part_6379_14909240.1114010266294
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Song doesn't have a constructor defined in that example. When you call=20
Blah.new, Ruby creates a new instance of Blah and then calls the method=20
initialize on that instance.

If you had:

class Song
attr_writer :name, :artist, :duration
def initialize name, artist, duration
@name, @artist, @duration =3D name, artist, duration
end
end

then calling Song.new("Title", "Artist", 56) would give you an object with=
=20
the appropriate values set. When you call Song.new(param1, param2, param3),=
=20
Ruby tries to pass those three parameters to the initialize method in Song.=
=20
But since initialize doesn't exist, it calls the default initialize, which=
=20
takes no parameters - that explains the error you got.

Hope that helps.


On 4/20/05, (e-mail address removed) <
=20
Greeting all,
=20
How about a refreshingly easy question for a change....
=20
Really, really basic question I know, but:
=20
class Song
attr_writer :name, :artist, :duration
end
=3D> nil
=20
song =3D Song.new("Blueberry Hill", "Fats Waller", 320)
=3D> ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize'
from (irb):24:in `new'
from (irb):24
=20
However....
=20
song =3D Song.new()
=3D> #<Song:0x2aab648>
=20
song.name <http://song.name> =3D "Blueberry Hill"
=3D> "Blueberry Hill"
=20
song.name <http://song.name>
=3D> "Blueberry Hill"
=20
Have I misread / misunderstood the text? Why the errors?
=20
SM
=20
(Only 770 pages to go... Woohoo!)
=20
=20
-------------------------------------------------------------------------= -----------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsolutions.com
Equinox Converged Solutions is a trading name of Synetrix Holdings=20
Limited.
=20
IMPORTANT NOTICE:
This message is intended solely for the use of the Individual or=20
organisation to whom it is addressed. It may contain privileged or=20
confidential information. If you have received this message in error, ple= ase=20
notify the originator immediately.
If you are not the intended recipient, you should not use, copy, alter, o= r=20
disclose the contents of this message. All information or opinions expres= sed=20
in this message and/or any attachments are those of the author and are no= t=20
necessarily those of Synetrix Holdings Limited.
Synetrix Holdings Limited accepts no responsibility for loss or damage=20
arising from its use, including damage from virus.
=20
-------------------------------------------------------------------------= ------------------
=20
=20


--=20
Bill Atkins

------=_Part_6379_14909240.1114010266294--
 
T

Tanner Burson

Greeting all,

How about a refreshingly easy question for a change....

Really, really basic question I know, but:

class Song
attr_writer :name, :artist, :duration
end
=> nil

song = Song.new("Blueberry Hill", "Fats Waller", 320)
=> ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize'
from (irb):24:in `new'
from (irb):24

However....

Okay what it's telling you is that your initialize method (that you
haven't defined) does not take any arguments, and you're passing it
three. Try this.
class Song
attr_writer :name, :artist, :duration
def initialize(name,artist,duration)
@name=name
@artist =artist
@duration = duration
end
song = Song.new()
=> #<Song:0x2aab648>

song.name = "Blueberry Hill"
=> "Blueberry Hill"

song.name
=> "Blueberry Hill"

Have I misread / misunderstood the text? Why the errors?

This section of code is indeed correct, as the attribute writers are
allowing you to put data into the "name" attribute. If you add the
constructor as listed above, it should work just fine.
 
D

David A. Black

Hi --

Greeting all,

How about a refreshingly easy question for a change....


Really, really basic question I know, but:

class Song
attr_writer :name, :artist, :duration
end
=> nil

song = Song.new("Blueberry Hill", "Fats Waller", 320)
=> ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize'
from (irb):24:in `new'
from (irb):24

However....

song = Song.new()
=> #<Song:0x2aab648>

song.name = "Blueberry Hill"
=> "Blueberry Hill"

song.name
=> "Blueberry Hill"

Have I misread / misunderstood the text? Why the errors?

You have to build up the example cumulatively -- see the #initialize
method on p. 325. (That's where you tell it to expect 3 args.)


David
 
K

kyu

Greeting all,

How about a refreshingly easy question for a change....


Really, really basic question I know, but:

class Song
attr_writer :name, :artist, :duration
end
=> nil

song = Song.new("Blueberry Hill", "Fats Waller", 320)
=> ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize'
from (irb):24:in `new'
from (irb):24

However....

song = Song.new()
=> #<Song:0x2aab648>

song.name = "Blueberry Hill"
=> "Blueberry Hill"

song.name
=> "Blueberry Hill"

Have I misread / misunderstood the text? Why the errors?

SM

(Only 770 pages to go... Woohoo!)

------------------------------------------------------------------------------------------
Equinox Converged Solutions
Tel: +44 (0)1252 405 600
http://www.equinoxsolutions.com
Equinox Converged Solutions is a trading name of Synetrix Holdings Limited.

IMPORTANT NOTICE:
This message is intended solely for the use of the Individual or organisation to whom it is addressed. It may contain privileged or confidential information. If you have received this message in error, please notify the originator immediately.
If you are not the intended recipient, you should not use, copy, alter, or disclose the contents of this message. All information or opinions expressed in this message and/or any attachments are those of the author and are not necessarily those of Synetrix Holdings Limited.
Synetrix Holdings Limited accepts no responsibility for loss or damage arising from its use, including damage from virus.
I'm still quite new to ruby, but to pass arguments to new, you need to define an initialize method: should look like this:

class Song

def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end

end

then blergh = Song.new("var1", "var2", "var3") should work :)
 
J

James Edward Gray II

You have to build up the example cumulatively -- see the #initialize
method on p. 325. (That's where you tell it to expect 3 args.)

Page 325 would be jumping quite a bit forward. ;) Try page 25, since
you've already passed that.

The book is building up a large example piece by piece. You need to
keep adding the new methods to Song to work through it.

Hope that helps.

James Edward Gray II
 
T

Tanner Burson

Page 325 would be jumping quite a bit forward. ;) Try page 25, since you've
Whoops, where did I get that '3' from? :)

It's usually right between the '4' and '2' on your keyboard ;)
 
J

Jason Foreman

For future reference, typing the code straight out of the book will
often result in these types of errors, as they use abbreviated code
listings to save space. If you kept typing each example from
beginning to end it would work ok, because you can add to a class at
any time. Its easier to check out the website [1] for the full
runnable code listings, in my opinion.

Jason

[1] http://www.pragmaticprogrammer.com/titles/ruby/code/
 
R

Ralf Müller

Greeting all,

How about a refreshingly easy question for a change....


Really, really basic question I know, but:

class Song
attr_writer :name, :artist, :duration
end
=> nil

song = Song.new("Blueberry Hill", "Fats Waller", 320)
=> ArgumentError: wrong number of arguments (3 for 0)
from (irb):24:in `initialize'
from (irb):24:in `new'
from (irb):24

However....

song = Song.new()
=> #<Song:0x2aab648>

song.name = "Blueberry Hill"
=> "Blueberry Hill"

song.name
=> "Blueberry Hill"

Have I misread / misunderstood the text? Why the errors?

SM

(Only 770 pages to go... Woohoo!)

I think, you should add the 'initialzie' function from page 25. Otherwise, the Class Song inherits its Constructor from 'Object'.

attr_writer :name, :artist, :duration

only gives you direct write access on the 3 attributes trough Song.attribute, but it does not keep you from writing 'initialze'.


All the Code examples of (at least) one chapter should be considered as ONE piece of code, if you try to get them run. Otherwise the authors would have to copy all the basic stuff into the examples for a special functionality, which would be pretty unreadyble.

regards
ralf
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top