← Back to Upcase

Difference between self and @

(charlieanna) #1

What is the difference between self.name and @name in case of a model class?

if I set @name = “ankur” inside a before_create filter then this does not work but self.name = “ankur” works.

(Derek Prior) #2

self.name is a method call whereas @name = is an instance variable assignment. I prefer to use the setters wherever possibe.

See: http://forum.thoughtbot.com/t/different-ways-of-initializing-variables-in-the-initialize-method/908/2?u=derekprior

(charlieanna) #3

Oh. Thanks

(Sean Griffin) #4

And then there’s CoffeeScript, which is just different enough to trip you up when you have to context switch from Ruby…