[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Simon.Mullis

4/20/2005 3:14:00 PM

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



13 Answers

Bill Atkins

4/20/2005 3:18:00 PM

0

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

If you had:

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

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

Hope that helps.


On 4/20/05, Simon.Mullis@equinoxsolutions.com <
Simon.Mullis@equinoxsolutions.com> wrote:
>
> 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 <http://son... = "Blueberry Hill"
> => "Blueberry Hill"
>
> song.name <http://son...
> => "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.equinoxsol...
> 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.
>
> -------------------------------------------------------------------------------------------
>
>


--
Bill Atkins

Tanner Burson

4/20/2005 3:18:00 PM

0

On 4/20/05, Simon.Mullis@equinoxsolutions.com
<Simon.Mullis@equinoxsolutions.com> wrote:
> 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

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



dblack

4/20/2005 3:20:00 PM

0

kyu

4/20/2005 3:20:00 PM

0

Simon.Mullis@equinoxsolutions.com wrote:

>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.equinoxsol...
>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 :)




James Gray

4/20/2005 3:25:00 PM

0

On Apr 20, 2005, at 10:19 AM, David A. Black wrote:

> 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



dblack

4/20/2005 3:49:00 PM

0

Tanner Burson

4/20/2005 4:03:00 PM

0

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

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

>
> David
>
> --
> David A. Black
> dblack@wobblini.net
>

>



Christian Neukirchen

4/20/2005 7:01:00 PM

0

<Simon.Mullis@equinoxsolutions.com> writes:

> 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

class Song < Struct.new(:name, :artist, :duration); end

Song.new("Blueberry Hill", "Fats Waller", 320)
=> #<struct Song name="Blueberry Hill", artist="Fats Waller", duration=320>

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


threeve.org

4/20/2005 7:40:00 PM

0

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



Ralf Müller

4/21/2005 5:58:00 AM

0

On Thu, 21 Apr 2005 00:13:34 +0900
<Simon.Mullis@equinoxsolutions.com> wrote:

> 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