4

What does a ruby method ending with an "=" mean?

See the available methods in this print out:

 2.2.0 :066 > obj.methods(false)
 => [:label=, :label, :description=, :description, :thumbnail=, :thumbnail, :attribution=, :attribution, :license=, :license, :logo=, :logo, :see_also=, :seeAlso=, :see_also, :seeAlso, :related=, :related, :within=, :within, :metadata=, :metadata, :sequences=, :sequences, :structures=, :structures, :viewing_hint=, :viewingHint=, :viewing_hint, :viewingHint, :viewing_direction=, :viewingDirection=, :viewing_direction, :viewingDirection, :service=, :service] 

For example whats this difference between label= and label?

Jeff
  • 3,943
  • 8
  • 45
  • 68
  • 2
    Next time consider waiting a little longer before selecting an answer, to allow others who are still preparing their answers to post them before you apply the checkmark. Here you made the selection a mere 20 minutes after posting the question, when only one answer had been given. – Cary Swoveland May 04 '15 at 00:04

4 Answers4

11

foo= is no different than any other method, except:

  • it requires precisely one argument and
  • Ruby permits you to add spaces before the = character.

class Foo
  def foo=(other)
    puts 'hi'
  end
end
Foo.new.foo                 = 7
hi

class Goo
  def goo=
    puts 'hi'
  end
end
Goo.new.goo=

Ruby says, "I'm waiting for an argument...". So we provide one:

4

and then she complains about what she asked you to do:

#=> ArgumentError: wrong number of arguments (1 for 0)

= methods are typically used to create a setter for an instance variable (if attr_acccessor or attr_writer is not used):

class Foo
  def initialize(foo)
    @foo=foo
  end
  # getter
  def foo
    @foo
  end
  # setter
  def foo=(other)
    @foo = other
  end
end

f = Foo.new('dog')
f.foo
  #=> "dog" 
f.foo = 'cat'
  #=> "cat" 
f.foo
  #=> "cat" 
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
3

the methods ending with "=" are setting the instance variable

look at the answer here: why-use-rubys-attr-accessor-attr-reader-and-attr-writer

Community
  • 1
  • 1
Shalev Shalit
  • 1,945
  • 4
  • 24
  • 34
  • 6
    It's important to note that while they *can* set an instance variable, they can also do other things: `def foo=(bar); puts "Not setting the instance variable"; end` – Ryan Bigg May 03 '15 at 23:33
2

It is equivalent of setter methods in other languages, it is just convention so it looks more natural to say

obj.description="Fun Site" 

vs

obj.setDescription("Fun Site")
aarti
  • 2,815
  • 1
  • 23
  • 31
1

There is nothing special about methods that end in =

You can see this by running the code below:

def bob=
   puts "bob="
end

p send('bob='.to_sym)

What is special is the '=' infix operator. When you write self.bob = "bill". It is interpreted as self.send('bob='.to_sym, "bill").

Putting a ? at the end of a method is a hint that it returns a boolean (true/false). Methods that end in ! hint that the method affects the instance. See String#chomp vs String#chomp.

You can find out more about ruby operators at http://www.tutorialspoint.com/ruby/ruby_operators.htm and more about naming conventions at https://github.com/bbatsov/ruby-style-guide#naming

G. Allen Morris III
  • 1,012
  • 18
  • 30
  • Until recently I thought all methods with names ending in `?` returned booleans, but there are at least two that don't (v2.2): [Numeric#nonzero?](http://ruby-doc.org/core-2.2.0/Numeric.html#method-i-nonzero-3F) and [Float#infinite?](http://ruby-doc.org/core-2.2.0/Float.html#method-i-infinite-3F). – Cary Swoveland May 13 '15 at 08:23