syntax - Defining methods with equals in Ruby

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?

This question and all comments follow the "Attribution Required."

All Answers

Answers Loading...
Leave a Reply
You must be logged in to post a answer.
About the author