I am a front end developer working mostly with javascript with a foundation in C/C++. Now I find myself working on RoR middleware for my company's Ruby on Rails team. Is there a "this" object in Ruby on Rails? For example: if I call user.method(), inside of the method's scope, can I reference the user object via "this.foo"... or would it be "self.foo"... or something entirely different? Thank you.
Asked
Active
Viewed 1,815 times
0
-
For more understanding about [Self](http://stackoverflow.com/questions/6669527/use-of-ruby-self-keyword) – Hetal Khunti Dec 10 '14 at 05:01
-
2In Ruby (and therefore in Rails), if a method doesn't have an explicit receiver, the receiver is assumed to be `self`. Suppose you want to add a method to the class `String` to reverse and capitalize a string. You could do this: `class String; def revup() reverse.upcase end; end`, `"abc".revup #=> "CBA"`. In the method `revup`, `reverse` does not have an explicit receiver, so `self.reverse.upcase => "abc".reverse.upcase => "CBA"` is executed. There are a few cases where you do need `self.` (e.g., when the method is an accessor or the method is `class`), which you will learn that in time. – Cary Swoveland Dec 10 '14 at 05:42
1 Answers
3
try this out:
self
is equal to this
in ruby.You can get current object in context using self
keyword.

Sachin Singh
- 7,107
- 6
- 40
- 80