Monday 6 June 2016

Ruby Symbols And More about Methods

Ruby Symbols:

# Symbols start with a colon 
:action.class == Symbol 
:action.to_s == "action" 
:action == "action".to_sym 
 
# There is only one instance of every symbol 
:action.equal?(:action) # => true 
'action'.equal?('action') # => false 
 
# Symbols are typically used as keys in hashes 
my_hash = {:controller => "home", :action => "index"} 

More about Methods:

  • Arbitrary number of arguments: def my_methods(*args)
  • Converting Array to arguments: my_method([a, b]*)
  • Dynamic method invocation: object.send(:method_name)
  • Duck typing: object.respond_to?(:method_name)
  • If the last argument is a Hash, the braces can be omitted: link_to("Home", :controller => "home")
 Hi readers,

hope you learn today post.

Leave comment or share.

No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...