Filippos wrote in post #1014053:
> wow thanx!
>
>
>
>>   puts "woof"
>>    'not known'
>>   end
>>  end
>>
>>  def show
>>   puts @color
>>   puts self.color *
>>  end
>> end
>
> * so in this context self.color calls the color method of the Dog
> class for the d object (d.color) . What if we omitted "self" , would
> rails execute the color method with the same result?
>

Yes, when a method is called without "a receiver", i.e without an object 
in front of it, then ruby calls the method with self.  In fact, all 
methods have to have a receiver, i.e an object that calls the method, so 
if you don't provide one, ruby uses self.  One time when you do have to 
explicitly write self is when you are on the left side of an equals 
sign:

color = "black"

v.

self.color = "black"

In the first case, you create a local variable and assign 'black' to it. 
In the second case, you call the color=() method.




>
>>
>> d = Dog.new
>> d.bark
>> d.show
>>
>> --output:--
>> woof
>> black    #@color
>> schwartz  #self.color
>>
>
>>
>> If you are interested in more examples, you can search google for
>> something like 'why
>> getters and setters'
>>
>
> Ok, i think i kind of understand what you mean here, but need some
> examples. I will google it.
> Getters and setters are the same with the attr_accessor but we can't
> use it in a controller because its an ActiveRecord attribute, right?
>

attr_accessor :color

is shorthand in ruby for this code:

def color=(val)
  @color = val
end

def color
  @color
end

You can call attr_accessor (yes it is a method) in any class.


> So inside the getter and setter we can define what is going to be
> stored to or retrieved from an instant variable.
>
> def online_user= (user)
> @current_user = user
> #or any other code we want to write
> end
>
> def online_user
> @current_user
> # or any other code we want to write
> end
>
> Must the name of the "methods" (online_user) be the same with the
> instant varialbe (@current_user) inside the "method" ?

The getter and setter method names will be the same as the instance 
variable name if you use attr_accessor.  If you write the methods by 
hand, you could use different names, but that is not done.  For 
instance, you wouldn't do this:


class Dog
  def color=(val)
    @age = val
  end

  def color
    @age
  end
end

d = Dog.new
d.color = 10
puts d.color

--output:--
10


I decided to try programming the app using the principles of the anti 
getter/setter crowd.  They say that instead of asking for a value with 
the getter, and then doing some calculation with the value, instead 
define a method in the class that does the calculation, and just call 
that method.  Following those principles, here is what my SessionsHelper 
module looks like:




module SessionsHelper

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    #self.current_user = user
    @current_user = user
  end

  def signed_in?
    get_user_from_cookie ? true : false
  end

  def sign_out
    cookies.delete(:remember_token)
    @current_user = nil
  end

  def get_user_from_cookie
    @current_user || begin
      cookie_array = cookies.signed[:remember_token]

      if cookie_array
        @current_user = User.authenticate_with_salt(*cookie_arr)
        return @current_user
      else
        return nil
      end

    end # || block
  end

end


Then instead of calling current_user in the various actions, tests, and 
helpers, I just call get_user_from_cookie.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to