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

OK... Thankyou all.

Much appreciated...

SM

...and things are just a little bit clearer...

···

-----Original Message-----
From: kyu [mailto:kyu@console-pimps.org]
Sent: 20 April 2005 16:20
To: ruby-talk ML
Subject: Re: [NEWBIE]: Example from Pickaxe2 gives errors... (p. 31)

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.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 :slight_smile:

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