Last Updated: February 25, 2016
·
411
· nlingutla

Variable assignment like functions in ruby

If you don't like the orthodox way of assigning value to the instance variables of a class like Object.setProperty(value), you can use ruby's syntactic sugar to just write something that looks like Object.property = value All you need to do is to have an instance method with name property=

For example, have a class like

Class Person
  def name=(name)
      @name = name
  end
end

Now you can create a new instance and assign name as following

nik = Person.new
nik.name=('nik')

or

nik.name='nik'

or even

nik.name = 'nik'