Ruby variable assigned to just the keyword "new"

Viewed 133

This is a hard one to search for, but I've got a line of Ruby in a class method where a variable is first declared by assigning it a bare new keyword and I'm not exactly sure what's going on here:

  def self.html_render_controller(post)
    controller = new
    controller.action = 'show'
    controller.post = post
    controller.render_to_string
  end

Coming from other languages, it looks to me like controller is implicitly being initialized as an object of some kind. And render_to_string is a Rails built-in, so my best guess is controller is initialized as some kind of Rails object?

I guess my question is really what is the value of controller after that first assignment? Or what kind of execution rules or even just documentation in Ruby would point me in the right direction for understanding this kind of shorthand?

3 Answers

It is not so weird as ruby code. It Is quite straight forward.

new is not a keyword is a method called on the implicit self. You do not need parenthesis to call a method in Ruby.

If you call a method without an explicit receiver in Ruby, the receiver is going to be self, inside the body of the method self.html_render_controller, self references the class where the method is defined.

So executing new inside the method self.html_render_controller just return a new object of the class where the method is defined.

You can have more details on new from the documentation on Class, that is the class of any class in Ruby, being a class just an object of the class Class.

I'm not exactly sure what's going on here

It's the new method of your class, i.e. it returns a new instance of the class your html_render_controller method is defined in.

Since you come from other languages, it might be easier to think of controller = new() (which in fact works just fine in Ruby, but it's more idiomatic to omit empty parentheses unless needed)

what kind of execution rules or even just documentation in Ruby would point me in the right direction for understanding this kind of shorthand?

This is Ruby's default method calling mechanism when omitting a receiver: "self is the default receiver. If you don't specify any receiver self will be used."

In instance methods, self refers to the current instance. In class methods, self refers to the class the method is defined in.

These three code snippets are all equivalent: *

class MyController
  def self.foo
    controller = new
  end
end

class MyController
  def self.foo
    controller = self.new
  end
end

class MyController
  def self.foo
    controller = MyController.new
  end
end

You could also replace the self after def with the class name:

class MyController
  def MyController.foo
    controller = MyController.new
  end
end

And using the latter, most explicit form, the method could even be defined outside the class body:

class MyController
end

def MyController.foo
  controller = MyController.new
end

* Note that the above examples are not identical. For example, with sub-classes, self.new would dynamically create an instance of the respective (sub-)class whereas MyController.new would always create an instance of MyController. And you usually want to define your methods within the class body to ensure proper constant lookup.

new is instantiating an object of the class where this html_render_controller method is.

You can change it to self.new instead of new, then it's clearer where this method is coming from. Since html_render_controller is a class method, self will be the class where the method is.

class Foobar
  def self.foo
    foobar = new
    # `foobar` is a new instance of Foobar
  end
end
Related