Hello All,
still a newbie in rails and would like to know when I shoud place a '@' before a variable name and when not ?
Regards,
Joel
Hello All,
still a newbie in rails and would like to know when I shoud place a '@' before a variable name and when not ?
Regards,
Joel
Hi Joel,
The @ symbol is (through my experience) generally used for sharing variables between controllers and views. So in your controller, you'd have something like:
@var = Users.find(...)
And in your view:
<%= "Hi, " + @var.first_name %>
But, again, that's just how I use it
It has nothing to do with convention.
@foo is an instance variable
foo is a local variable
http://www.ruby-doc.org/docs/ProgrammingRuby/
In particular: Programming Ruby: The Pragmatic Programmer's Guide
joel wrote:
Hello All,
still a newbie in rails and would like to know when I shoud place a '@' before a variable name and when not ?
Regards,
Joel
See here: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html
Ok Great !
Thanks Guys for these resources !
Joel
foo could also be a method!
Be wary of the fact that local variables take precedence over methods, as shown in this example:
>> foo = "woot" => "woot" >> def foo >> "bar" >> end => nil >> foo => "woot"
To avoid that you can just write foo() i.e. with the parentheses, if you want to call the function.
OK ! Nice information guys !
Thanks !
Joel
foo could also be a method!
Be wary of the fact that local variables take precedence over methods, as shown in this example:
A common mistake related to this is
class Foo attr_accessor :bar def some_method bar = true end end
This does not call the accessor, and does not set @bar (only the local
variable bar).
Fred