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 开发者_C百科controllers?
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.
Can it be declaration of nested class that will not be used anywhere else?
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.
精彩评论