ruby - Check if a method responds to another method in Rails? -


my controller:

class dashboardcontroller < applicationcontroller   before_action :show_dashboard_menu, only: :index    def index     ...   end    private    def show_dashboard_menu     true   end end 

i want spread method show_dashboard_menu across several controllers , check if need show menu particular actions helper method:

my helper:

def show_dashboard_menu?   true if current_action responds show_dashboard_menu end 

and use in view show or hide dashboard menu:

show_dashboard_menu? 

ruby objects have method called respond_to? (take care if methods , attributes)

2.2.1 :001 > 1.respond_to?("to_s")  => true  2.2.1 :002 > 1.respond_to?("each")  => false  2.2.1 :003 > [].respond_to?("each")  => true  2.2.1 :004 > [].respond_to?("something")  => false  2.2.1 :005 >  

or can allso ask methods or public_methods

2.2.1 :010 > 2.methods.include?(:real)  => true  2.2.1 :011 > 2.public_methods.include?(:real)  => true  2.2.1 :012 > 2.methods  => [:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :bit_length, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :remainder, :real?, :nonzero?, :step, :quo, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]  

so in applicationcontroller able like

def show_dashboard_menu?   self.respond_to? "show_dashboard_menu" end 

Comments