/developer|entrepreneur/i
Always looking for new developer talent, even those with zero experience, as you never know who's got the potential to become a great developer.
These are also called "accessors" (read) and "mutators" (write) in other languages, but the principle is the same. Useful terminology for those coming to Ruby from places where those terms are used instead.
Ruby's way of declaring them as x= type methods is fairly unique and makes for some extremely concise code since there's no need for getX / setX pairs, it's just x and x=.
Another thing worth mentioning is if you have a "setter" or attr_writer you can't use that without prefixing it with some kind of object, even self.
For example:
class Example
attr_accessor :test
def assign!
test = :assigned
end
end
example = Example.new
example.assign!
example.test
# => nil
That's because in the code test = :assigned creates a variable named test, it doesn't call the test= method. To use those you must do self.test = :assigned inside the context of that method or example.test = :assigned by using some kind of variable for reference.
This leads to a lot of confusion in places like ActiveRecord where assigning to the auto-generated attributes "doesn't work".
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
These are also called "accessors" (read) and "mutators" (write) in other languages, but the principle is the same. Useful terminology for those coming to Ruby from places where those terms are used instead.
Ruby's way of declaring them as
x=
type methods is fairly unique and makes for some extremely concise code since there's no need forgetX
/setX
pairs, it's justx
andx=
.Another thing worth mentioning is if you have a "setter" or
attr_writer
you can't use that without prefixing it with some kind of object, evenself
.For example:
That's because in the code
test = :assigned
creates a variable namedtest
, it doesn't call thetest=
method. To use those you must doself.test = :assigned
inside the context of that method orexample.test = :assigned
by using some kind of variable for reference.This leads to a lot of confusion in places like ActiveRecord where assigning to the auto-generated attributes "doesn't work".