Vamsi
Vamsi

Reputation: 278

Can some one explain the code snippet given below in ruby?

class ApplicationController < ActionController::Base
  class InvalidParam < StandardError;end
end

What is the reason for including another class inside application controller?? What will be the behavior for other controllers?

Upvotes: 0

Views: 112

Answers (3)

Chris McCauley
Chris McCauley

Reputation: 26363

Adding Semantics - Private exceptions v System generated exceptions

There's no structural difference between InvalidParam and StandardError - I'm betting that the developer is layering his/her own semantics on InvalidParam. That's naughty because it will just confuse the reader.

The code declares a nested class called InvalidParam for the purposes of Exception handling. The developer wants to be able to raise and rescue exceptions with InvalidParam rather than StandardError - most likely because they want to distinguish between system exceptions and their own.

Upvotes: 1

clyfe
clyfe

Reputation: 23770

The reason for including another class inside application controller is that the class will be visible only inside the controller (scoping). Other controllers will not be influenced.

Upvotes: 0

Karel Frajt&#225;k
Karel Frajt&#225;k

Reputation: 4489

Can it be declaration of nested class that will not be used anywhere else?

Upvotes: 0

Related Questions