: 
http://rubylearning.com/
http://rubylearning.com/satishtalim/tutorial.html
http://rubylearning.com/download/downloads.html
http://rubylearning.com/other/certification.html
http://rubylearning.com/blog/
http://rubylearning.com/other/ruby_news.html
http://rubylearning.com/other/testimonials.html
http://rubylearning.com/jobs/ruby_jobs.html
http://rubylearning.com/other/ruby_gurus.html
http://rubylearning.com/satishtalim/services.html
http://rubylearning.com/contact/contact.html
http://rubylearning.com/satishtalim/about.html


Syntactic sugar

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's 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 example - p050newdog.rb

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:

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=


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:


nd.name=('Benzy')


Here's the modified example -


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')

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

nd.name=('Benzy')

 

you're allowed to do this:

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.

 

Method calls using the equal-sign syntax are common in Rails applications.




Learning Ruby Navigation                                              <Access Control  | TOC  |  Mutable and Immutable Objects>