In my sparetime I am learning Ruby on Rails right now and I want to start a little series about some things that helped me understand that programming language. So lets start with the first episode.

You are new to Ruby ( on Rails ) and wonder what those different looking symbols for variables and methods in Ruby mean?

@name,  name,


def,  def foo

What’s the difference and why does it seem like no one else is being confused by this stuff. That stuff doesn’t seem to be explained well anywhere: So here I wanna try to explain it from all I figured out so far from so many different websites.

def is a class method. For example like:

class User …
def self.login(…)
login = User.login(…)

def foo is an instance method named foo.

@foo is used inside a class definition to mean “foo variable” means “calling foo method” of this object.
The confusion may come from the fact that instance variables and getter methods have the same name.
Remember that an instance variable is always prefixed by ‘@’. Anything else is a method call.
Another subtle place to get confused is a local variable. Suppose we have:

def foo
cow = 1

What is cow? It is a local variable. cow will exist until foo returns. Actually… that gets confusing too. Because Ruby has closures. Lets not think about that just yet.
An instance variable @foo will live as long as the object does. A local variable foo lives as long as the function does.

That’s pretty much it for the first episode.