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

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

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://song.name> = "Blueberry Hill"
=> "Blueberry Hill"

song.name <http://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.

-------------------------------------------------------------------------------------------

--
Bill Atkins

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.

···

On 4/20/05, Simon.Mullis@equinoxsolutions.com <Simon.Mullis@equinoxsolutions.com> wrote:

SM

Hi --

···

On Thu, 21 Apr 2005 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?

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

--
David A. Black
dblack@wobblini.net

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:

···

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

<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://chneukirchen.org

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

···

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!)

Page 325 would be jumping quite a bit forward. :wink: 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

···

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

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/

Hi --

···

On Thu, 21 Apr 2005, James Edward Gray II wrote:

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. :wink: Try page 25, since you've already passed that.

Whoops, where did I get that '3' from? :slight_smile:

David

--
David A. Black
dblack@wobblini.net

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

Whoops, where did I get that '3' from? :slight_smile:

It's usually right between the '4' and '2' on your keyboard :wink:

···

David

--
David A. Black
dblack@wobblini.net