Callable and Runnable
objects in Ruby.
By: Rahul Bajaj
@rabajaj_
About Me! Associate Software Engineer @ Red Hat
Follow me:
Twitter: @rabajaj_
Github: rahulbajaj0509
@rabajaj_
Agenda
● Proc Objects
● Lambdas
● Method Objects
@rabajaj_
1. Callable objects in Ruby:
2. Threads in Ruby
Blocks
Can you do something like this?
block = do |test|
puts test
end
3.upto(8) block
@rabajaj_
Obviously not!
But why?
● Blocks are not objects
● They can not be stored in to a
variable
Does ruby have a solution for
this?
@rabajaj_
Procs
foo=Proc.new do |test|
puts test
end
3.upto(8) { foo.call("hello world") }
@rabajaj_
Procs
● Basics of creating and using
Procs.
● Relationship between procs
and blocks.
● Procs as closures
● Proc parameters & arguments
@rabajaj_
Relationship between
procs and blocks.
def capture_block(&block)
block.call
end
capture_block { puts "Inside the block" }
Block to Proc
Using Procs for Blocks
def capture_block(&block)
block.call
end
p = Proc.new { puts “This proc arg will serve as code
block” }
capture_block(&p)
@rabajaj_
Inside the block
This proc arg will serve as code block
closures
def call_some_proc(pr)
a = “irrelevant ‘a’ in method scope”
puts a
pr.call
end
a = “ ‘a’ to be used in Proc block”
pr = Proc.new { puts a }
pr.call
call_some_proc(pr)
‘a’ to be used in Proc block
irrelevant ‘a’ in method scope
‘a’ to be used in Proc block
@rabajaj_
Proc parameters &
arguments
pr = Proc.new { |x| puts “Called with argument #{x}”}
pr.call(100)
Called with argument 100
>> pr = Proc.new { |x| puts x}
=> #<Proc:975676592328@>
>> pr.call
nil
>> pr.call(1,2,3)
1
@rabajaj_
Lambdas
The lambda method returns a
Proc object!
● Basic creation of Lambda.
● Difference between Proc and
Lambda:
a. Calling
b. Return method
c. Arguments
@rabajaj_
Return statement:
def return_test
l=lambda{return}
l.call
puts “Still here!”
p = Proc.new { return }
p.call
puts “You won’t see this”
end
return_test
Still here!
Lambdas
Calling:
lam = lambda { puts “A
Lambda!” }
lam.call
A Lambda!
@rabajaj_
Lambdas
continued..
Arguments:
>> lam = lambda { |x| puts x }
=> #<Proc:093846783543@>
>>lam.call(1)
1
>>lam.call
ArgumentError: wrong number of
arguments
@rabajaj_
Threads
Thread creation
Thread.new do
puts "Starting the thread"
sleep 1
puts "At the end of the thread"
end
puts "Outside the thread"
@rabajaj_
Threads
continued..
t = Thread.new do
puts "Starting the thread"
sleep 1
puts "At the end of the thread"
end
puts "Outside the thread"
t.join
@rabajaj_
puts "Trying to read in some files..."
t = Thread.new do
(0..2).each do |n|
begin
File.open("part0#{n}") do |f|
text << f.readlines
end
rescue Errno::ENOENT
puts "Message from thread: Failed on n=#{n}"
Thread.exit
End
end
end
t.join
puts "Finished!"
@rabajaj_
Trying to read in some files...
Message from thread: Failed on n=1
Finished!
THREAD TERMINATION (KILLING)
The idea is to read the contents of the three
files (part00, part01, part02) into the string
text. If any of the files isn’t found, the thread
terminates.
Thank you !
@rabajaj_

Callable and runnable objects in ruby

  • 1.
    Callable and Runnable objectsin Ruby. By: Rahul Bajaj @rabajaj_
  • 2.
    About Me! AssociateSoftware Engineer @ Red Hat Follow me: Twitter: @rabajaj_ Github: rahulbajaj0509 @rabajaj_
  • 3.
    Agenda ● Proc Objects ●Lambdas ● Method Objects @rabajaj_ 1. Callable objects in Ruby: 2. Threads in Ruby
  • 4.
    Blocks Can you dosomething like this? block = do |test| puts test end 3.upto(8) block @rabajaj_
  • 5.
    Obviously not! But why? ●Blocks are not objects ● They can not be stored in to a variable Does ruby have a solution for this? @rabajaj_
  • 6.
    Procs foo=Proc.new do |test| putstest end 3.upto(8) { foo.call("hello world") } @rabajaj_
  • 7.
    Procs ● Basics ofcreating and using Procs. ● Relationship between procs and blocks. ● Procs as closures ● Proc parameters & arguments @rabajaj_
  • 8.
    Relationship between procs andblocks. def capture_block(&block) block.call end capture_block { puts "Inside the block" } Block to Proc Using Procs for Blocks def capture_block(&block) block.call end p = Proc.new { puts “This proc arg will serve as code block” } capture_block(&p) @rabajaj_ Inside the block This proc arg will serve as code block
  • 9.
    closures def call_some_proc(pr) a =“irrelevant ‘a’ in method scope” puts a pr.call end a = “ ‘a’ to be used in Proc block” pr = Proc.new { puts a } pr.call call_some_proc(pr) ‘a’ to be used in Proc block irrelevant ‘a’ in method scope ‘a’ to be used in Proc block @rabajaj_
  • 10.
    Proc parameters & arguments pr= Proc.new { |x| puts “Called with argument #{x}”} pr.call(100) Called with argument 100 >> pr = Proc.new { |x| puts x} => #<Proc:975676592328@> >> pr.call nil >> pr.call(1,2,3) 1 @rabajaj_
  • 11.
    Lambdas The lambda methodreturns a Proc object! ● Basic creation of Lambda. ● Difference between Proc and Lambda: a. Calling b. Return method c. Arguments @rabajaj_
  • 12.
    Return statement: def return_test l=lambda{return} l.call puts“Still here!” p = Proc.new { return } p.call puts “You won’t see this” end return_test Still here! Lambdas Calling: lam = lambda { puts “A Lambda!” } lam.call A Lambda! @rabajaj_
  • 13.
    Lambdas continued.. Arguments: >> lam =lambda { |x| puts x } => #<Proc:093846783543@> >>lam.call(1) 1 >>lam.call ArgumentError: wrong number of arguments @rabajaj_
  • 14.
    Threads Thread creation Thread.new do puts"Starting the thread" sleep 1 puts "At the end of the thread" end puts "Outside the thread" @rabajaj_
  • 15.
    Threads continued.. t = Thread.newdo puts "Starting the thread" sleep 1 puts "At the end of the thread" end puts "Outside the thread" t.join @rabajaj_
  • 16.
    puts "Trying toread in some files..." t = Thread.new do (0..2).each do |n| begin File.open("part0#{n}") do |f| text << f.readlines end rescue Errno::ENOENT puts "Message from thread: Failed on n=#{n}" Thread.exit End end end t.join puts "Finished!" @rabajaj_ Trying to read in some files... Message from thread: Failed on n=1 Finished! THREAD TERMINATION (KILLING) The idea is to read the contents of the three files (part00, part01, part02) into the string text. If any of the files isn’t found, the thread terminates.
  • 17.