Super noobish question...

...about methods that end with an equal sign

Page 1 of 1

6 Replies - 1155 Views - Last Post: 31 August 2009 - 02:08 PM Rate Topic: -----

#1 THX1138  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-December 07

Super noobish question...

Post icon  Posted 31 August 2009 - 10:08 AM

Hi everybody,
The question I'm going to ask is probably the most noobish question that you will have heard for a long time but everybody has to start somewhere..

What I don't really understand is the following:

How come methods that finish with an equal sign can be called with a space before the equal. For instance look at the very bad example below:
old_age = 23
def age=(new_age)
	old_age = new_age
end

p age = 12


The reason why I ask the question is because often times there are two methods with the same name and one of which ends with an equal sign, and for a beginner it's hard to recognize which is being used...

Is This A Good Question/Topic? 0
  • +

Replies To: Super noobish question...

#2 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Super noobish question...

Posted 31 August 2009 - 10:57 AM

old_age is "variable", age= is often instance method.

So, if you have class Person:

class Person
  
  def age
    @person_age
  end

  def age=(arg)
    @person_age = arg
  end

end

#Used like this

mike = Person.new
mike.age = 20 # use age= method, often called setter
puts mike.age  # use age method, often called getter


In many languages these methods are called setter and getter, one to set instance variable, one to get the instance variable. At first it doesn't make sense, but later you'll figure it out when you start using objects and classes.

Also, in RUby there's shortcut to create setters and getters:

class Person
  attr_accessor :age
end


attr_accessor will create them on the fly. attr_reader will create only getter, attr_writer will create only.

Note that in your method "age=" old_age is a local variable , so the value of your old_age variable that you declared above won't change.

This post has been edited by MitkOK: 31 August 2009 - 11:02 AM

Was This Post Helpful? 1
  • +
  • -

#3 THX1138  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-December 07

Re: Super noobish question...

Posted 31 August 2009 - 01:16 PM

Thanks for your reply. The setter/getter notions does make sense (to me). What is still blurry is the following:

the name of the method is age=. Even though now I know that it's the setter, the space between the equal and the age is still weird; here is how I see it:

the age= method can't be written ag e= but it can be called age =
Both spaces do not make sense to me, but age = does work (ag e= doesn't for obvious reasons).
Hence the question: why does age = works when logically speaking only age= should work?

(the answer to that question might be completely useless, but I'm really curious...)

This post has been edited by THX1138: 31 August 2009 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Super noobish question...

Posted 31 August 2009 - 01:32 PM

This is how I think the interpreter works :

You cannot define setter like age =, because Ruby will think, that your defining method age with argument "=" ( remember that parenthesis in Ruby method definition or invocation are optional, so you can say foo 2 or foo(2) ) and "=" is not valid argument.
def age=(arg) # method name is age=
  @person_age = arg
end

def age =(arg)
  #method name is age with arguments =(arg), which is not valid
  # it's the same as def age (=(arg)), which I said is not valid
  @person_age
end


When you invoke the method on object, f.e mike.age =, Ruby interpreter ignores the space and calls the setter method.

We know that in Ruby everything is an object, but for beginners it doesn't make sense.

In other languages we have operators ( + - / * ), in Ruby they are just methods on objects:
1 + 2 # it's syntactic sugar for 1.+(2), in other words - call + method on 1 ( which is fixnum object ) with argument 2.


It's not easy to realize how object system in Ruby works if you have written apps in C++ or Java for example, but when you get it - voila, it's so obvious, logical, easy and beautiful.

This post has been edited by MitkOK: 31 August 2009 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#5 THX1138  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-December 07

Re: Super noobish question...

Posted 31 August 2009 - 02:00 PM

I learned Java in college, I know PHP, and I tried to learn C and Python and stopped out of laziness; my point is, even though I have very little experience in programming (since I haven't used all those languages a lot) I still know what you mean :). For some reason I have a bit of problems understanding some of the automatisms some Rubyist have...

View PostMitkOK, on 31 Aug, 2009 - 12:32 PM, said:

Ruby interpreter ignores the space and calls the setter method

Is it safe to consider that allowing an empty space at the end of the method name before the equal sign (for methods which name end with an equal sign that is) is a feature of the ruby language, like optional parenthesis?
Before this would be a good enough answer for me :)
Was This Post Helpful? 0
  • +
  • -

#6 MitkOK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 32
  • View blog
  • Posts: 403
  • Joined: 09-August 07

Re: Super noobish question...

Posted 31 August 2009 - 02:03 PM

It's Ruby VM feature :)
Was This Post Helpful? 1
  • +
  • -

#7 THX1138  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 14-December 07

Re: Super noobish question...

Posted 31 August 2009 - 02:08 PM

ok now it's completely clear in my head; I just needed "some" kind of logic attached to that annoying space before the equal :P
Thanks a lot for your help and your patience!

This post has been edited by THX1138: 31 August 2009 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1