ruby, two ways how to pass params to proc -


i looked through code , found author passes params block using []. tryed myself

my_proc = proc { |x| x + 1 } = 0 my_proc[a]        # => 1 my_proc.call(a)   # => 1 

what difference between 2 calls? syntax sugar?

both ways same , aliases each other. thus, both variants call same method not determined special syntax. defined as:

class proc   def call(*args)     #...   end    alias [] call end 

you might interested note there third way:

my_proc.(a) 

this syntactic sugar (i.e. extension of syntax of ruby language language). objects accepting #call can "called" way , ruby ensures invoke call method.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -