[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

What is attr_accessor?

Phper

4/30/2008 5:21:00 AM

I even don't know what attr_accessor is.
5 Answers

Christopher Swasey

4/30/2008 5:32:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Apr 30, 2008 at 1:25 AM, tenxian <hi.steven.tu@gmail.com> wrote:

> I even don't know what attr_accessor is.


#attr_accessor is a method of Module.
http://ruby-doc.org/core/classes/M...

Perhaps you might want to pickup a copy of the pickaxe book (
http://www.pragprog.com/titles/ruby/progra...). It will firmly ground
you in the fundamentals of the language.

(attr_accessor, by the way, simply defines setter and getter methods for
each symbol passed as a parameter)

Christopher

Phillip Gawlowski

4/30/2008 5:42:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tenxian wrote:
| I even don't know what attr_accessor is.

There is this wonderful thing called 'search engine', which helps in
answering questions:

http://www.google.com/search?client=safari&rls=en&q=ruby%20attr_accessor&ie=UTF-8&am...

In short: attr_accessor is shorthand to define getters and setters.

For example:

attr_accessor :my_variable creates the methods to assign and to read the
instance variable '@my_variable'. Which saves you from defining these
methods yourself.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

~ Well, it just seemed wrong to cheat on an ethics test. -- Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgYBroACgkQbtAgaoJTgL95FgCgjSEHqzPTfgNJybsKzqNMnp51
9tgAnjPFMLHhpXHOAe5hQDJsx8VTbxU3
=kdo5
-----END PGP SIGNATURE-----

7stud --

4/30/2008 6:44:00 AM

0

tenxian wrote:
> I even don't know what attr_accessor is.

class Dog

def initialize(a_name)
@name = a_name
end

def name #get the name
return @name
end

def name=(a_name) #set the name
@name = a_name
end

end


d = Dog.new("Spot")
puts d.name #Spot

d.name = "Red"
puts d.name #Red


======compare to: ============

class Dog
attr_accessor :name

def initialize(a_name)
@name = a_name
end

end


d = Dog.new("Spot")
puts d.name #Spot

d.name = "Red"
puts d.name #Red




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

Ron Fox

4/30/2008 11:00:00 AM

0

http://www.ruby-doc.org/docs/UsersGuide/rg/acce...


tenxian wrote:
> I even don't know what attr_accessor is.

Marc Heiler

4/30/2008 3:26:00 PM

0

> I even don't know what attr_accessor is.

attr_accessor :colour in your class would allow you do to this:


class Dog
attr_accessor :colour
end


jimmy = Dog.new
jimmy.colour = 'brown'

puts jimmy.colour # "brown"
jimmy.colour = 'black'

puts jimmy.colour # "black"



As far as i know the name attr_accessor is chosen because it unites both
attr_reader (getter method) and attr_writer (setter method). (There also
exists attr :foo but I never use attr )
--
Posted via http://www.ruby-....