1

I have this line of Ruby code generated by rails:

class PostsController < ApplicationController 

What does < mean?

1
  • 2
    Seems like there are answers enough, however I didn't see a reference to the official Ruby doc yet. So here you go: ruby-doc.org/core-2.5.1/doc/syntax/… Commented Aug 27, 2018 at 14:05

4 Answers 4

6

< is used for Inheritance. In Ruby, a class can only inherit from a single other class.

class PostsController < ApplicationController

in above line of code PostsController (child-class) is inherits from ApplicationController parent-class.

In Rails:

Action Controllers are the core of a web request in Rails. By default, only the ApplicationController in a Rails application inherits from ActionController::Base. All other controllers inherit from ApplicationController. This gives you one class to configure things such as request forgery protection and filtering of sensitive request parameters.

for more info :

Sign up to request clarification or add additional context in comments.

Comments

1

What you're saying there is: "Declare a new class called PostsController and inherit the behaviour from ApplicationsController to be used in PostsController".

Basically < is used for Inheritance

More info here

Comments

1

It means that PostsController definition starts by having everything in ApplicationController. while the rest of the definitions will add/replace members/attributes to PostsController.

1 Comment

Using the term "extend" in your sense should be avoided in the context of Ruby because it means something else. It is misleading.
0

Using < indicates inheritance. Basically, this means that PostsController will have everything ApplicationController has, except for it's private members. However, you can override methods in the subclass to change the behavior of the methods inherited from the superclass.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.