Last Updated: February 25, 2016
·
613
· bryanmikaelian

Rails...it is just Ruby

DHH has mentioned this several times: "Rails is just Ruby." It is true and it is beautiful how it works out.

I recently got reminded of this when we were rewriting some devise controllers in our app. Let's look at an example:

class SessionsController < Devise::SessionsController
  def new
   // do amazing new action stuff specific to your app
  end

  def create
   // sign in to the app.  create action fun specific to your app
  end
end

// Assumes routes are mapped to these actions

It is your basic devise sessions controller. What happens if you need to do a new version of this controller but you don't want to duplicate all the valid actions (Say we need a new layout, handle other types of requests and add a few more actions)? Turns out Rails is quite crafty if you change the routes to point to a new controller that inherits from the former controller...

class V2::SessionsController < SessionsController
   def promotion_sign_in

   end

end

// Assumes new routes are set up

After changing all your routes, you'd be surprised to know that new and create still work. This is because of the simple nature of inheritance. Pretty awesome!

It seems kind of like a "duh" thing but I haven't thought about this when working in Rails apps. However, Ruby doesn't see Action Controller controllers as anything special. They are just classes.