Being new to Ruby, I’m having trouble explaining to myself the behavior around method definitions within Ruby.
The example is noted below…
class Foo
def do_something(action)
action.inspect
end
def do_something_else=action
action.inspect
end
end
?> f.do_something("drive")
=> "\"drive\""
?> f.do_something_else=("drive")
=> "drive"
The first example is self explanatory. What Im trying to understand is the behavior of the second example. Other than what looks to be one producing a string literal and the other is not, what is actually happening? Why would I use one over the other?
Generally,
do_somethingis a getter, anddo_something=is a setter.is equivalent to
To answer your question about the difference in behavior, methods that end in
=always return the right hand side of the expression. In this case returningaction, notaction.inspect.