Posts

Showing posts with the label proc

lambda proc and block in rails

this all things basically we used in block level coding and for some custom queries in rails model class Main   [ 1 , 2 , 3 ].each do | num |     puts num   end end def block_method   yield end block_method { puts "Test"} def arg_method   yield   yield 1 end arg_method { |i| puts i*1 rescue nil} # explicit block def explicit_block(&block)   block.call #same as yield end explicit_block {puts "This is explicit block"} # lambda lambda_test = -> {puts "This is lambda"} lambda_test.call lambda_test.() lambda_test[] # lambda with args arg_lambda = -> (i){ puts i} arg_lambda.call("test") my_proc = Proc.new { |x| puts x } my_proc.call # Should work my_lambda = -> { return 1 } puts "Lambda result: #{my_lambda.call}" # Should raise exception my_proc = Proc.new { return 1 } puts "Proc result: #{my_proc.call}" def call_proc   puts "Before proc"   my_proc = Proc.n