VirtuosiMedia
VirtuosiMedia

Reputation: 53356

Syntax For Calling Methods In Rails 3

I come from a PHP background and I'm used to calling a class' internal methods with $this->methodName(), but I can't seem to find the syntax for doing the same in Rails. I want to do something like the following in a controller:

class Foo

  def bar
   #call self.baz
  end

  def baz
    #some code
  end

end

What is the proper syntax for the method call? Also, if there is a good place for just learning basic syntax for Ruby/Rails, please share. I'm finding it frustrating just trying to find simple syntax features.

Upvotes: 0

Views: 532

Answers (2)

zetetic
zetetic

Reputation: 47548

Ruby's syntax is pretty simple - just call .method_name on the object:

foo = Foo.new
foo.bar # calls bar on foo

Within a method defintion self (which Rubyists call the "receiver") is implicit, so just use the method name:

def bar
  baz # calls Foo#baz
end

You can also use self explicitly to do the same thing:

def bar
  self.baz # also calls Foo#baz
end

A good introduction to Ruby syntax can be found here.

Upvotes: 1

Hauleth
Hauleth

Reputation: 23566

Use simply baz or self.baz. self is equivalent to $this in PHP.

Upvotes: 1

Related Questions