문제

I found an example code:

def callback(procs)
  procs[:var_1]
  puts "Proceed"
  procs[:var_2]
end

callback(:var_1 => Proc.new {block_1},
         :var_2 => Proc.new {block_2})

I can't figure out what is in square brackets [:var_1] means. Is this some way to call Porc/lambda? I'm confused also because of the Hash-like way for creating Procs in:

callback(:start => Proc.new {puts "The begining of the CALLBACK"},
         :finish => Proc.new {puts "The ending of the CALLBACK"})

I would appreciate any help on this matter.

도움이 되었습니까?

해결책

The method callbacks is build so it is receiving a hash. This hash stores procs against each key, hance to execute the proc you need to fatch it from hash using its key:

hash = { :start => Proc.new {puts "The begining of the CALLBACK"},
         :finish => Proc.new {puts "The ending of the CALLBACK"} }

hash[:start]       #=> proc object you can call `call` on.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top