[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attr_reader

7stud 7stud

9/1/2007 3:24:00 AM

On p. 30-31 of "Programming Ruby (2nd ed)", there is this example:

class Song
attr_reader :name, :artist, :duration
end

and the text says:

"The corresponding instance variables, @name, @artist, and @duration,
will be created for you."

But I discovered that I still have to define an initialize method to
create objects:

class Song
attr_reader :x, :y, :z

def initialize(x, y, z)
@x = x
@y = y
@z = z
end
end

s = Song.new(10, 20, 30)
puts s.x

So, it seems like initialize is what creates the instance variables.
Or, does attr_reader somehow do its thing before initialize is called?
If so, why is it considered important to know that attr_reader creates
the instance variables instead of initialize?
--
Posted via http://www.ruby-....

12 Answers

7stud 7stud

9/1/2007 3:58:00 AM

0

Peña, Botp wrote:
> From: 7stud 7stud [mailto:dolgun@excite.com]
> # "The corresponding instance variables, @name, @artist, and @duration,
> # will be created for you."
>
> that should read: "The corresponding instance variable getter and setter
> methods will be created for you."
>

Huh? As far as I can tell, no setter methods are created:

class Song
attr_reader :x, :y, :z

def initialize(x, y, z)
@x = x
@y = y
@z = z
end
end

s = Song.new (10, 20, 30)
puts s.x
s.x = 40

--output:--
10
r1test.rb:22: undefined method `x=' for #<Song:0x253c8 @y=20, @x=10,
@z=30> (NoMethodError)


>
> #So, it seems like initialize is what creates the instance variables.
>
> not really.
>
> irb(main):097:0> class Song2
> irb(main):098:1> attr_accessor :x, :y
> irb(main):099:1> end
> => nil
>
> note, no initialization there
>

My program has no attr_accessor line (the book hasn't mentioned
attr_accessor yet). Your saying the initialize method in my program
doesn't create the instance variables? What does?

--
Posted via http://www.ruby-....

Peña, Botp

9/1/2007 4:08:00 AM

0

From: 7stud *** [mailto:dolgun@excite.com]
# Huh? As far as I can tell, no setter methods are created:
#
# class Song
# attr_reader :x, :y, :z

sorry, i misread your post. attr_reader only gives getters.

# My program has no attr_accessor line (the book hasn't mentioned
# attr_accessor yet). Your saying the initialize method in my program
# doesn't create the instance variables?

my english is poor. sorry. initialize will create it, but it's not the only way. you can create your own setters.

kind regards -botp

7stud 7stud

9/1/2007 4:17:00 AM

0

Ok. Thanks.
--
Posted via http://www.ruby-....

7stud 7stud

9/1/2007 11:48:00 PM

0

Actually, I think attr_reader does create the instance variables:

class Song
attr_reader :name, :artist, :duration
end

song = Song.new

puts song.name
puts song.artist
puts song.duration

puts song.fake

--output:---
nil
nil
nil
r1test.rb:18: undefined method `fake' for #<Song:0x253c8>
(NoMethodError)
--
Posted via http://www.ruby-....

7stud 7stud

9/1/2007 11:54:00 PM

0

And this confirms it:

class Song
#attr_reader :name, :artist, :duration
end

song = Song.new

puts song.name
puts song.artist
puts song.duration

puts song.fake

--output:---
r1test.rb:8: undefined method `name' for #<Song:0x25648> (NoMethodError)
--
Posted via http://www.ruby-....

Logan Capaldo

9/2/2007 12:08:00 AM

0

On 9/1/07, 7stud *** <dolgun@excite.com> wrote:
> Actually, I think attr_reader does create the instance variables:
>
> class Song
> attr_reader :name, :artist, :duration
> end
>
> song = Song.new
>
> puts song.name
> puts song.artist
> puts song.duration

Nope. It's just that an undefined instance variable is treated as
though it were nil. Try this on for size:

puts song.name
puts song.instance_variables

7stud 7stud

9/2/2007 4:14:00 AM

0

Logan Capaldo wrote:
> On 9/1/07, 7stud *** <dolgun@excite.com> wrote:
>> puts song.duration
> Nope. It's just that an undefined instance variable is treated as
> though it were nil.

Then why isn't song.fake treated as though it were nil?


--
Posted via http://www.ruby-....

7stud 7stud

9/2/2007 4:19:00 AM

0

Logan Capaldo wrote:
>Try this on for size:
>
> puts song.name
> puts song.instance_variables

lol. I looked up instance_variables in "Programming Ruby (2d ed)" and it
says:

"Note that simply defining an accessor does not create the corresponding
instance variables."

That statement contradicts the statement I posted earlier on p. 31 of
the same book.

--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/2/2007 8:14:00 AM

0

7stud -- wrote:
> Logan Capaldo wrote:

> > Nope. It's just that an undefined instance variable is treated as
> > though it were nil.
>
> Then why isn't song.fake treated as though it were nil?

song.fake isn't an instance variable, it's a method (well actually it's not,
it's nothing, but if it did exists, it'd be a method).
attr_reader :duration creates the method duration, which returns the
instance variable @duration, even if the latter doesn't exist yet.
song.fake throws an error because there is no method fake, independantly
of whether or not an instance variable @fake exists.
song.instance_variable_get "@fake" would return nil.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

7stud 7stud

9/2/2007 9:04:00 AM

0

Sebastian Hungerecker wrote:
> attr_reader :duration creates the method duration, which returns the
> instance variable @duration, even if the latter doesn't exist yet.
> song.fake throws an error because there is no method fake

Ok. Thanks for the explanation.
--
Posted via http://www.ruby-....