To clarify Arup Rakshit's answer a little more:
Ruby allows having some "weird" characters in its methods, that you would normally not see in other languages. The most notably ones are ?
, !
and =
.
So, you can define the methods:
def answered?()
not @answer.nil?
end
def answer!()
@answer = Answer.new
end
def answer()
@answer
end
def answer=(answer)
@answer = answer
end
For someone coming from, say, PHP, this looks strange. But really they are all different methods! I have added brackets everywhere, to clarify my point, but in Ruby, you can leave them off, so when a method does not take arguments,people generally leave them off.
def answer()
end
def answer
end
Are the same.
The last two, the answer()
and answer=()
, are often refered to as getters and setters. The first is often used to test something binary, like has_question?
or valid?
. The second is referred to as a bang method, answer!
, more often used in methods that modify itself, like some_array.merge!(other_array)
, which modifies some_array
.