About the "def method=(param)",why compiled error?

Z

Zengqh Mansion

very simple program:

class Song
def initialize(duration)
@duration =3D duration
end

def duration=3D(new_duration)
@duration =3D new_duration
end
end

song =3D Song.new(260)
print song.duration


the compiler outputs=EF=BC=9A
D:/=E6=88=91=E7=9A=84=E6=96=87=E6=A1=A3/Ruby/attr_writer.rb:12:in `<main>=
': undefined method `duration'
for
#<Song:0xb33f10 @duration=3D260> (NoMethodError)=E3=80=82

appreciate for your help.

-- =

Posted via http://www.ruby-forum.com/.=
 
P

Peter Zotov

very simple program:
=20
class Song
def initialize(duration)
@duration =3D duration
end
=20
def duration=3D(new_duration)
@duration =3D new_duration
end
end
=20
song =3D Song.new(260)
print song.duration
=20
=20
the compiler outputs=EF=BC=9A
D:/=E6=88=91=E7=9A=84=E6=96=87=E6=A1=A3/Ruby/attr_writer.rb:12:in `<main>= ': undefined method
`duration' for
#<Song:0xb33f10 @duration=3D260> (NoMethodError)=E3=80=82
=20
appreciate for your help.
=20

You have only defined a "duration=3D" method on your class, and an
instance variable "@duration". Calling "song.duration" means invoking a
method "duration" on object "song", which is not defined in class Song.
You can define it this way:

class Song
<skip>
def duration
@duration
end
end

Or, in more concise way, this way:

class Song
def initialize(duration)
@duration =3D duration
end
attr_accessor :duration
end

Executing attr_accessor helper method in a class context is the same as
the definition of two functions, "duration" and "duration=3D", as they're
described above. You can also use attr_reader and attr_writer method; I
hope their function is obvious.

--=20
WBR, Peter Zotov
 
Z

Zengqh Mansion

thanks very much.
i was reading the book "programming ruby".
it said "def attribute=(new_attribute)", can be used attribute as if
it's public variables, i think i was wrong.
 
J

Jiawei Yong

Zengqh Mansion wrote in post #982758:
thanks very much.
i was reading the book "programming ruby".
it said "def attribute=(new_attribute)", can be used attribute as if
it's public variables, i think i was wrong.

Do not have the book, but I'm guessing that statement was made in a
particular context.

def attribute=(new_attribute) does allow you to use attribute as a
public variable, when you are setting new value for attribute.

If you have subsequently proceeded to code def attribute as Peter has
suggested above, that will allow you to use your attribute as a public
variable when it comes to getting value of attribute.

Nevertheless, attr_accessor, attr_reader and attr_writer should be
betterfor getter and setter.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top