Ruby on Rails Sunday, July 31, 2011

Filippos wrote in post #1014028:

> 1. So when we want to go to localhost/myapp/users/1 rails first maps
> the URL to Users#show, then creates the object user_controller =
> UsersController.new and then executes the user_controller.show?
>

Yes.

> 2. When we assign an instance variable inside the controller , doesn't
> it refer to the controller object that rails create?

Yes. Instance variables attach themselves to an object--that object is
the object that calls the method in which the instance variable is first
set. Here is an example:

class Dog
def bark
@color = "black"
puts "woof"
end

def color #getter method
@color
end
end

d = Dog.new
d.bark
puts d.color

--output:--
woof
black


> For example @user
> -> user_controller = UsersController.new
> What would the difference be between the instance variable and the
> self.user inside the controller method.
>

Lets see:

class Dog
def bark
@color = "black"
puts "woof"
end

def color
translate_to_german(@color)
end

def translate_to_german(word)
if word == "black"
'schwartz'
else
'not known'
end
end

def show
puts @color
puts self.color
end
end


d = Dog.new
d.bark
d.show

--output:--
woof
black #@color
schwartz #self.color

> 3. I didn't understand your justification of getter and setter.Since
> instance variables work fine why would you need to define a setter and
> getter.

Because it's good programming practice.

> You can just assign to the instance variable and you're done
> since it will be accessible in the controller and view for as long as
> the sess_controller lasts.
>

Yes, but what if later you decide that you want to alter the value that
is assigned to the instance variable before doing the assignment? Then
you would have to look through your code and find every line where you
have written @var_name = ...., and change it. What if your program was
10 million lines long? Would you want to do that? How long would it
take you? Two years? Twenty years?

If you always access instance variables using getters and setters, you
have the flexibility to change the value that is set or the value that
is retrieved. For instance suppose you were doing calculations in feet
and you decided you needed to do the calculations in meters. Without
changing the user interface, you could make those changes in the getter
and setter methods. The user could still enter the value in feet, and
then your
setter could convert to meters and save that value instead. Your getter
could then convert the value back to feet and return it.

If you are interested in more examples, you can search google for
something like 'why
getters and setters'

> Instead of self.current_user = user we could write @current_user =
> user
>
> Why call the current_user getter and not just call the @current_user
> directly.
>
> I'm a bit confused.
>
> The easy way would be to memorize or copy/paste but understanding the
> code is better way to create rails apps.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment