2 Replies - 2781 Views - Last Post: 22 May 2012 - 01:27 PM Rate Topic: -----

#1 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Need some help understanding this method - Assignment in method def

Posted 26 April 2012 - 03:01 PM

I'm currently going through Michael's Hartl Rails tutorial and I ran across this which I can not understand and would like some input from the bright minds here as to what is really going on. I have never seen an assignment in a method def. The tutorial doesn't elaborate enough for me to understand. Thanks in advance.

module SessionsHelper

  def sign_in(user)
    .
    .
    .
  end

  def current_user=(user)
    @current_user = user
  end
end


This post has been edited by rvbsanjose: 26 April 2012 - 03:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Need some help understanding this method - Assignment in method def

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Need some help understanding this method - Assignment in method def

Posted 27 April 2012 - 01:57 AM

The line def current_user=(user) defines a method named "current_user=" with a parameter called "user". In ruby a method's name may contain a "=" as its last character. Such methods may be called using the syntax object.foo = bar. So when you see a statement of that form, it will call the method foo= with the argument bar on the object object, which is to say that a statement of the form foo.bar = baz is always a method call.
Was This Post Helpful? 2
  • +
  • -

#3 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: Need some help understanding this method - Assignment in method def

Posted 22 May 2012 - 01:27 PM

Note that @ defines an instance variable. You're also going to want to look into accessors soon for determining the read/write options of the instance variables.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1