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
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
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.
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:
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.)
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
···
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.
-------------------------------------------------------------------------------------------
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
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?
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.