Reputation: 622
Im currently doing some online tutorials about the ruby programming langauge and I think the explanations/examples I have been given thus far are lacking. I have two examples Id like to show you before directly asking the question.
The first example is:
Traditional Getters/Setters;
class Pen
def initialize(ink_color)
@ink_color = ink_color # this is available because of '@'
end
# setter method
def ink_color=(ink_color)
@ink_color = ink_color
end
# getter method
def ink_color
@ink_color
end
end
And the second example is:
ShortCutt Getter/Setters;
class Lamp
attr_accessor :color, :is_on
def initialize(color, is_on)
@color, @is_on = color, false
end
end
Ok, So for the first example I think its pretty straight forward. I am 'initializing' an accessible variable throughout my entire Lamp class called "@ink_color". If I wanted to set "@ink_color" to red or blue I would simply call my 'Setter' method and pass 'red' or 'blue' to the parameter (ink_color) in my setter. Then If I wanted to access or 'Get/Getter' the color I have 'Set/setter' I would call my getter method and ask for 'ink_color'.
The second example makes sense to me as well; Instead of typing out what the getter and setter methods look like, ruby provides a 'shortcut' that essentially runs code to build the getter and setter for you.
But heres the question - How do I reverse engineer the 'shortcut' version? Lets say I was looking at my above shortcut example and wanted to do it the "traditional" way without a shortcut?
Would the reverse engineering of the "shortcut" look something like the below code(my attempt that doesn't seem right to me)....
My Attempt/Example
class Lamp
def initialize(color, is_on)
@color = color
@is_on = is_on
end
def color=(color)
@color = color
end
def is_on=(is_on)
@is_on = is_on
end
def color
@color
end
def is_on
@is_on
end
end
Is my attempt right/workable code? It just seems like im missing a piece conceptually when it comes to this getter/setter stuff.
Upvotes: 1
Views: 1630
Reputation: 6872
These are Ruby's getters and setters shortcut. It works like C# properties, that injects the get_Prop
(getter) and set_Prop
(setter) methods.
attr_accessor
: injects prop
(getter) and prop=
(setter) methods.attr_reader
: it's a shortcut for read-only properties. Injects prop
method. The prop
value can only be changed inside the class, manipulating the instance variable @prop
.attr_writer
: it's a shortcut for write-only properties. Injects prop=
method.Ruby doesn't have methods called get_prop
(getter) and set_prop
(setter), instead, they're called prop
(getter) and prop=
(setter).
That being said, you can infer that
class Person
attr_accessor :name, :age
end
is the short version for
class Person
# getter
def name
return @name
end
# setter
def name=(value)
@name = value
end
end
You don't need to call return
, Ruby methods returns the last executed statement.
If you are using Ruby on Rails gem, you can build model objects using new
and passing properties values as arguments, just like:
p = Person.new(name: 'Vinicius', age: 18)
p.name
=> 'Vinicius'
That's possible because Rails injects something like this initialize
method to ActiveRecord::Base
and classes that includes ActiveModel::Model
:
def initialize(params)
params.each do |key, value|
instance_variable_set("@#{key}", value)
end
end
Upvotes: 4