I even don't know what attr_accessor is.
#attr_accessor is a method of Module.
http://ruby-doc.org/core/classes/Module.html
Perhaps you might want to pickup a copy of the pickaxe book (
Pragmatic Bookshelf: By Developers, For Developers). 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
···
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.
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&oe=UTF-8
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.blogspot.com
~ Well, it just seemed wrong to cheat on an ethics test. -- Calvin
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-forum.com/\.
http://www.ruby-doc.org/docs/UsersGuide/rg/accessors.html
tenxian wrote:
···
I even don't know what attr_accessor is.
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-forum.com/\.