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
On Mon, Jul 28, 2008 at 4:10 PM, joel [email protected] wrote:
still a newbie in rails and would like to know when I shoud place a
‘@’ before a variable name and when not ?
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:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html
–
Greg D.
http://destiney.com/
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
=> nilfoo
=> “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
On 29 Jul 2008, at 01:55, Ryan B. wrote:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs