What does the equal sign ('=') do when it comes after the method name in the method definition?

I saw this in the screencast and just wondered what the "=" symbol does in this case.

def express_token=(token) ... end 

I would understand if it was something like this -

 def express_token(token = nil) 

The above (second code snippet) means setting zero as the default value for the token parameter. However, in the first code snippet, '=' is outside the brackets.

Thanks in advance.

+49
ruby
Mar 22 2018-11-23T00:
source share
4 answers

This fragment defines a Virtual attribute (or setter method), so that the expression "express_token" looks like an attribute, although this is just the name of the method, For example:

 class Foo def foo=(x) puts "OK: x=#{x}" end end f = Foo.new f.foo = 123 # => 123 # OK: x=123 

Note that the “f” object does not have an attribute or instance variable named “foo” (and is also not needed), so the “foo =” method is just syntactic sugar that allows you to call a method that looks like an assignment. Also note that such setter methods always return their argument regardless of any return or end value.

If you define a set-level setter method, for example, in "irb", then the behavior can be a bit confusing due to the implicit addition of methods to the Object class. For example:

 def bar=(y) puts "OK: y=#{y}" end bar = 123 # => 123, sets the variable "bar". bar # => 123 Object.new.bar = 123 # => 123, calls our method # OK: y=123 Object.public_methods.grep /bar/ # => ["bar="] 
+65
Mar 22 '11 at 23:12
source share

These methods allow you to install vars instances in a more indirect way: imagine that you have a Person class

 class Person < ActiveRecord::Base attr_accessible :first_name, :last_name def full_name [@first_name, @last_name].join end def full_name=(name) @first_name, @last_name = name.split(" ") end end 

Then you can do something like this:

 p = Person.new p.full_name = "John Doe" p.first_name # => "John" p.last_name # => "Doe" p.full_name # => "John Doe" 
+18
Mar 22 '11 at 23:08
source share

This is actually part of the function name. So this is a setter if you need separate functionality than the default for getters and setters.

+3
Mar 22 2018-11-23T00:
source share

Let's look at the following example:

 class NewDog def initialize(breed) @breed = breed end # create reader only attr_reader :breed, :name # setter method def set_name(nm) @name = nm end end nd = NewDog.new('Doberman') nd.set_name('Benzy') puts nd.name 

If you reorganize the setter method to this:

 def name=(nm) @name = nm end 

other programmers know that the name = method behaves like a setter method. Like show by @maerics, it behaves like a virtual attribute.

The result is as follows:

 class NewDog def initialize(breed) @breed = breed end # create reader only attr_reader :breed, :name # setter method def name=(nm) @name = nm end end nd = NewDog.new('Doberman') nd.name = 'Benzy' puts nd.name 
+1
Nov 22 '16 at 10:11
source share



All Articles