Ruby beginner question 2

A

Alucard

Hi all!

I'm using ruby 1.82. This is the code copy from www.ruby-lang.org,
programming ruby.

And I found that both the following code cannot run, with the following
error occur:

song_2.rbw:4:in `initialize': wrong number of arguments (3 for 0)
(ArgumentError)
from song_2.rbw:4:in `new'
from song_2.rbw:4

So, what is going wrong?

Thanks in advance!

CODE:
class Song
def name
@name
end
def artist
@artist
end
def duration
@duration
end
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.artist
aSong.name
aSong.duration

#--------------------------------------------------------------------------------------------

class Song
attr_reader :name, :artist, :duration
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.artist
aSong.name
aSong.duration
 
J

Jeff Wood

--------------060502030402090405010707
Content-Type: text/plain; charset=Big5
Content-Transfer-Encoding: 7bit
Hi all!

I'm using ruby 1.82. This is the code copy from www.ruby-lang.org,
programming ruby.

And I found that both the following code cannot run, with the following
error occur:

song_2.rbw:4:in `initialize': wrong number of arguments (3 for 0)
(ArgumentError)
from song_2.rbw:4:in `new'
from song_2.rbw:4

So, what is going wrong?

Thanks in advance!

CODE:
class Song
def name
@name
end
def artist
@artist
end
def duration
@duration
end
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.artist
aSong.name
aSong.duration

#--------------------------------------------------------------------------------------------

class Song
attr_reader :name, :artist, :duration
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.artist
aSong.name
aSong.duration
Your objects have no constructor method ... therefore, it's simply
relying on the constructor from Object ( which is implicitly inherited ).

So, simply add

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

within either of your class definition examples... then they'll work.

The inherited constructor from Object takes no parameters, and that's
why you are getting the "initialize wrong number of arguments 3 for
0"... You're passing 3 to a no argument constructor...

Hope that helps.

j.


--------------060502030402090405010707--
 
R

Robert Klemme

Jeff said:
#-------------------------------------------------------------------------
-------------------
Your objects have no constructor method ... therefore, it's simply
relying on the constructor from Object ( which is implicitly
inherited ).

So, simply add

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

within either of your class definition examples... then they'll work.

The inherited constructor from Object takes no parameters, and that's
why you are getting the "initialize wrong number of arguments 3 for
0"... You're passing 3 to a no argument constructor...

A simpler alternative is to just use Struct:

# direct
Song = Struct.new :name, :artist, :duration

class Song
# other methods
end

Kind regards

robert
 
B

balcer

Robert Klemme napisał(a):
A simpler alternative is to just use Struct:

# direct
Song = Struct.new :name, :artist, :duration

class Song
# other methods
end

Kind regards

robert

Sometimes, during the proces of teaching it is not a good idea
to give and alternative :)

Jacek
 
C

Chris Game

balcer said:
Sometimes, during the proces of teaching it is not a good idea
to give and alternative :)

Especially as the OP was on p30 of the book, and 'Struct' hasn't
come up yet!
 
G

Gavin Kistner

A simpler alternative is to just use Struct:

# direct
Song = Struct.new :name, :artist, :duration

class Song
# other methods
end

Wait wait wait...I never grokked Struct before.

Is it really just a convenient factory for generating classes with a
set of accessor properties, and a constructor that accepts zero or
more of those properties in order?

Holy crap, I see that Song.class is in fact Class.

I may finally get it! Re-reading the ri documentation for Struct, I
see that the above is sort of what it's trying to say. But the
wording always made me believe that the returned object was some sort
of non-class class. Some half-breed freakiness.


The combination of the name and documentation threw me off before. I
suppose 'Struct' sort of works, but something like
'AttributeClass' (while wordier and not as memorable). So apparently
I have no better suggestion for the name. But for the documentation...

At a minimum, I suggest replacing the first sentence:
"A +Struct+ is a convenient way to bundle a number of attributes
together, using accessor methods, without having to write an explicit
class."

Maybe I'm the only one (am I the only one?) but that makes it sound
like the return value isn't a class. Instead, I suggest something like:

The +Struct+ class provides a convenient way to quickly create a
class that contains a set of attributes. In addition to predefining
the accessor methods for these attributes, the returned class
contains other instance methods for accessing and modifying the
attributes. (These methods are documented below as instance methods
of the Struct class, but are in fact methods available to instances
of the class returned from the call to Struct.new).

For example:
User = Struct.new( :first, :last, :email )
p User.class #=> Class
p User.superclass #=> Struct

nobody = User.new
gk = User.new( 'Gavin', 'Kistner' )
p nobody #=> #<struct User first=nil, last=nil, email=nil>
p gk #=> #<struct User first="Gavin", last="Kistner",
email=nil>
p gk.size #=> 3
gk.email = '(e-mail address removed)'

p gk.members #=> ["first", "last", "email"]
p gk.values #=> ["Gavin", "Kistner", "(e-mail address removed)"]
p gk.values_at(1..2) #=> ["Kistner", "(e-mail address removed)"]
p gk.first #=> "Gavin"
p gk['last'] #=> "Kistner"

class User
def fullname
"#{first} #{last}"
end
end

p gk.fullname #=> "Gavin Kistner"




(I think it's very helpful to have an example usage at the very top
of the documentation, showing a simple usage with a type of object
that everyone understands. Obviously, it doesn't have to be my name
or email, though ;)

Also, the documentation for the Struct#values_at method seems to have
been lifted from the Array#values_at method, without changing the
example code to use a Struct rather than array. The example should be
something like:

Stuff = Struct.new( :a, :b, :c, :d, :e, :f )
chars = Stuff.new( 'a1', 'b1', 'c1', 'd1', 'e1', 'f1' )
p chars.values_at(1, 3, 5) #=> ["b1", "d1", "f1"]
p chars.values_at(-1, -3, -5) #=> ["f1", "d1", "b1"]
p chars.values_at(1..3, 2...5) #=> ["b1", "c1", "d1", "c1", "d1", "e1"]
p chars.values_at(1,7) #=> offset 7 too large for struct(size:6)
(IndexError)


(Aside: Has anyone ever used the #values_at method of the Struct
class? If so, what for?)


cc: ruby-doc mailing list
 
G

Gavin Kistner

--Apple-Mail-3-378692910
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed

(These methods are documented below as instance methods of the
Struct class, but are in fact methods available to instances of the
class returned from the call to Struct.new)

That one sentence should probably read something like:
(These methods, such as #values, are documented below as instance
methods of the Struct class; as the returned class inherits from
Struct, they are available instances of the class returned from the
call to Struct.new.)

--Apple-Mail-3-378692910--
 
B

BearItAll

Wait wait wait...I never grokked Struct before.

Is it really just a convenient factory for generating classes with a
set of accessor properties, and a constructor that accepts zero or
more of those properties in order?

Holy crap, I see that Song.class is in fact Class.

I may finally get it! Re-reading the ri documentation for Struct, I
see that the above is sort of what it's trying to say. But the
wording always made me believe that the returned object was some sort
of non-class class. Some half-breed freakiness.


The combination of the name and documentation threw me off before. I
suppose 'Struct' sort of works, but something like
'AttributeClass' (while wordier and not as memorable). So apparently
I have no better suggestion for the name. But for the documentation...

At a minimum, I suggest replacing the first sentence:
"A +Struct+ is a convenient way to bundle a number of attributes
together, using accessor methods, without having to write an explicit
class."

Maybe I'm the only one (am I the only one?) but that makes it sound
like the return value isn't a class. Instead, I suggest something like:

Yes, it's just you that missed that one.

Sorry, put the handkerchief away, I was fibbing, I didn't really get that
part either. In fact I believe they is a hidden bible of Ruby somewhere
that all these other users have copies of but we poor souls are left to
struggle. Perhaps they all worship at the feet of The Great Rudy
Blubber oops I mean Great Ruby Buddha (sorry, it's my accent), so are
rewarded with nice things such as snippets of code with explanations in
it.

But I'm darn well determined, I am certain that Ruby-rails is a good thing
so certain that although I can't get anything at all in ruby-rails to
work at the moment, I'm going to keep trying.
 
R

Robert Klemme

Gavin said:
A simpler alternative is to just use Struct:

# direct
Song = Struct.new :name, :artist, :duration

class Song
# other methods
end

Wait wait wait...I never grokked Struct before.

Is it really just a convenient factory for generating classes with a
set of accessor properties, and a constructor that accepts zero or
more of those properties in order?

Holy crap, I see that Song.class is in fact Class.

I may finally get it! Re-reading the ri documentation for Struct, I
see that the above is sort of what it's trying to say. But the
wording always made me believe that the returned object was some sort
of non-class class. Some half-breed freakiness.


The combination of the name and documentation threw me off before. I
suppose 'Struct' sort of works, but something like
'AttributeClass' (while wordier and not as memorable). So apparently
I have no better suggestion for the name. But for the documentation...

At a minimum, I suggest replacing the first sentence:
"A +Struct+ is a convenient way to bundle a number of attributes
together, using accessor methods, without having to write an explicit
class."

Maybe I'm the only one (am I the only one?) but that makes it sound
like the return value isn't a class. Instead, I suggest something
like:

The +Struct+ class provides a convenient way to quickly create a
class that contains a set of attributes. In addition to predefining
the accessor methods for these attributes, the returned class
contains other instance methods for accessing and modifying the
attributes. (These methods are documented below as instance methods
of the Struct class, but are in fact methods available to instances
of the class returned from the call to Struct.new).

For example:
User = Struct.new( :first, :last, :email )
p User.class #=> Class
p User.superclass #=> Struct

nobody = User.new
gk = User.new( 'Gavin', 'Kistner' )
p nobody #=> #<struct User first=nil, last=nil, email=nil>
p gk #=> #<struct User first="Gavin", last="Kistner",
email=nil>
p gk.size #=> 3
gk.email = '(e-mail address removed)'

p gk.members #=> ["first", "last", "email"]
p gk.values #=> ["Gavin", "Kistner", "(e-mail address removed)"]
p gk.values_at(1..2) #=> ["Kistner", "(e-mail address removed)"]
p gk.first #=> "Gavin"
p gk['last'] #=> "Kistner"

class User
def fullname
"#{first} #{last}"
end
end

p gk.fullname #=> "Gavin Kistner"




(I think it's very helpful to have an example usage at the very top
of the documentation, showing a simple usage with a type of object
that everyone understands. Obviously, it doesn't have to be my name
or email, though ;)

+10 for the doc addenum - I went through the same some time ago. It took
me smoe experimenting to full grasp the power and beauty of Struct.

Also, I found the option to name the struct by providing a first parameter
quite irritating. I haven't found any uses for this yet.
=> # said:
Also, the documentation for the Struct#values_at method seems to have
been lifted from the Array#values_at method, without changing the
example code to use a Struct rather than array. The example should be
something like:

Stuff = Struct.new( :a, :b, :c, :d, :e, :f )
chars = Stuff.new( 'a1', 'b1', 'c1', 'd1', 'e1', 'f1' )
p chars.values_at(1, 3, 5) #=> ["b1", "d1", "f1"]
p chars.values_at(-1, -3, -5) #=> ["f1", "d1", "b1"]
p chars.values_at(1..3, 2...5) #=> ["b1", "c1", "d1", "c1", "d1",
"e1"] p chars.values_at(1,7) #=> offset 7 too large for
struct(size:6) (IndexError)


(Aside: Has anyone ever used the #values_at method of the Struct
class? If so, what for?)

I never used it but it makes Struct a bit more compatible to Array. Note
also:
Foo = Struct.new :name, :val => Foo
f=Foo.new "n", "v"
=> # said:
f[0] => "n"
f[1] => "v"
f.each {|x| p x}
"n"
"v"
name=>n
val=>v
=> #<struct Foo name="n", val="v">>> Foo.ancestors
=> [Foo, Struct, Enumerable, Object, Kernel]

IOW, classes created by Struct.new are Enumerable and support an Array
like interface. :)

Kind regards

robert
 
R

Robert Klemme

BearItAll said:
On Mon, 22 Aug 2005 21:37:43 +0900, Gavin Kistner wrote:


Yes, it's just you that missed that one.
:)))

Sorry, put the handkerchief away, I was fibbing, I didn't really get
that part either. In fact I believe they is a hidden bible of Ruby
somewhere that all these other users have copies of but we poor souls
are left to struggle. Perhaps they all worship at the feet of The
Great Rudy
Blubber oops I mean Great Ruby Buddha (sorry, it's my accent), so are
rewarded with nice things such as snippets of code with explanations
in it.

Erm... Ruby Buddha is in fact just an alias for ruby-talk. :) That's
how *I* got to know Struct anyway.
But I'm darn well determined, I am certain that Ruby-rails is a good
thing so certain that although I can't get anything at all in
ruby-rails to work at the moment, I'm going to keep trying.

That's the spirit!

Kind regards

robert
 
R

Randy Kramer

Sometimes, during the proces of teaching it is not a good idea
to give and alternative :)

As a newbie / learner (but not the original OP), I am happy to have the
alternative presented, especially if it is simpler or better in some way
(maybe even better for teaching, but generally worse in other ways). I may
be overwhelmed, but I have the choice to pay attention to the alternative or
not.

Randy Kramer

PS: Just to complain, you had to quote all that other text to make your
comment?
 
G

Gaston Garcia

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

I would say, to not use the alternative right now. I think the guys in the=
=20
book lead us through a buch of steps for a reason.=20

=20

=20
As a newbie / learner (but not the original OP), I am happy to have the
alternative presented, especially if it is simpler or better in some way
(maybe even better for teaching, but generally worse in other ways). I ma= y
be overwhelmed, but I have the choice to pay attention to the alternative= =20
or
not.
=20
Randy Kramer
=20
PS: Just to complain, you had to quote all that other text to make your
comment?
=20
=20


--=20
-gaston

------=_Part_758_1039973.1124724909764--
 
C

Chris Game

Gaston said:
I would say, to not use the alternative right now. I think the
guys in the book lead us through a buch of steps for a reason.

That being said the book could do with proper editing. As it is
several concepts are used without proper explanation (binding,
scope, precedence,...)
 
R

Robert Klemme

Chris said:
That being said the book could do with proper editing. As it is
several concepts are used without proper explanation (binding,
scope, precedence,...)

If you are referring to the Pickaxe II: it's not meant as an introduction
to programming. The terms you name are all fairly basic and people with
background in SE or programming know them or can look them up easily (in
the Pickaxe as well as online).

Kind regards

robert
 
A

Alucard

Hi, J. It works!
After adding your code, the final workable version:

class Song
attr_reader :name, :artist, :duration
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end

aSong = Song.new("songName", "songArt", 260)
puts aSong.name
puts aSong.artist
puts aSong.duration

Thanks for your help!
 
C

Chris Game

Robert said:
If you are referring to the Pickaxe II: it's not meant as an
introduction to programming.

The 'road map' section clearly states it's for beginners as well as
expert users.
The terms you name are all fairly basic and people with background
in SE or programming know them or can look them up easily (in the
Pickaxe as well as online).

Basic terms could (and should, to avoid possible confusion) be
clearly defined in a short section then couldn't they?

And I can't say I like the condescending tone of your response.
 
R

Robert Klemme

Chris said:
The 'road map' section clearly states it's for beginners as well as
expert users.

As far as I can see it does not state that it tries to teach basics of
programming - basics of Ruby, yes.
Basic terms could (and should, to avoid possible confusion) be
clearly defined in a short section then couldn't they?

That depends on the audience of the book. For me it's obvious that it's
not targeted at people with zero experience with software development.
Ruby specific explanations are given in the introductory section. Of
course there may be errors and omissions but I think the explanations you
request do not belong there. My 0.02 EUR.
And I can't say I like the condescending tone of your response.

<frankly>Sometimes I don't like the apparent unwillingness of people to
dig up some information by themselves as well.</frankly>

Kind regards

robert
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top