Found this in
http://rubylearning.com/satishtalim/ruby_syntactic_sugar.html
Describes
def methodname= usage
Programmers use the term syntactic sugar to refer to special rules that
let you write your code in a way that doesn't correspond to the normal
rules but that is easier to remember how to do and looks better.
Let us say we want to set the name of a dog. As a starting point, name
can be set along with everything else at object creation time, as in the
example below.
1. class NewDog
2. def initialize(breed, name)
3. @breed = breed
4. @name = name
5. end
6. attr_reader :breed, :name # create reader only
7. end
8. nd = NewDog.new('Doberman', 'Benzy')
9. puts nd.name
class NewDog def initialize(breed, name) @breed = breed @name = name end
attr_reader :breed, :name # create reader only end nd =
NewDog.new('Doberman', 'Benzy') puts nd.name
Let's write a set_name method that allows us to set, or reset, the name
of an existing dog. We'll also rewrite the initialize method so that it
doesn't expect a name:
1. class NewDog
2. def initialize(breed)
3. @breed = breed
4. end
5. attr_reader :breed, :name # create reader only
6. # setter method
7. def set_name(nm)
8. @name = nm
9. end
10. end
11. nd = NewDog.new('Doberman')
12. nd.set_name('Benzy')
13. puts nd.name
class NewDog def initialize(breed) @breed = breed end attr_reader
:breed, :name # create reader only # setter method def set_name(nm)
@name = nm end end nd = NewDog.new('Doberman') nd.set_name('Benzy') puts
nd.name
Ruby allows you to define methods that end with an equal sign (=). Let's
replace set_name with a method called name=
1. def name=(nm)
2. @name = nm
3. end
def name=(nm) @name = nm end
name= does exactly what set_name did, and in spite of the slightly odd
method name, you can call it just like any other method:
1. nd.name=('Benzy')
nd.name=('Benzy')
Here's the modified example - p050newdog.rb
1. class NewDog
2. def initialize(breed)
3. @breed = breed
4. end
5. attr_reader :breed, :name # create reader only
6.
7. # setter method
8. def name=(nm)
9. @name = nm
10. end
11. end
12.
13. nd = NewDog.new('Doberman')
14. #nd.name=('Benzy')
15. nd.name = 'Benzy'
16. puts nd.name
class NewDog def initialize(breed) @breed = breed end attr_reader
:breed, :name # create reader only # setter method def name=(nm) @name =
nm end end nd = NewDog.new('Doberman') #nd.name=('Benzy') nd.name =
'Benzy' puts nd.name
The equal sign gives you that familiar "assigning a value to something"
feeling, so you know you're dealing with a setter method. It still looks
odd, but Ruby takes care of that, too.
Ruby gives you some syntactic sugar for calling setter methods. Instead
of this:
1. nd.name=('Benzy')
nd.name=('Benzy')
you're allowed to do this:
1. nd.name = 'Benzy'
nd.name = 'Benzy'
When the interpreter sees the message "name" followed by " =", it
automatically ignores the space before equal sign and reads the single
message "name=" - a call to the method whose name is name=, which we've
defined. As for the right-hand side: parentheses are optional on single
arguments to methods, so you can just put 'Benzy' there and it will be
picked up as the argument to the name= method.
IN RAILS: Method calls using the equal-sign syntax are common in Rails
applications.
--
Posted via http://www.ruby-forum.com/.