#! /usr/bin/env ruby
def amethod(arg=10)
yield 2
arg * 3
end
def bmethod(&block)
block.call
end
def cmethod(block)
block.call
end
p = Proc.new { puts "I am a proc object" }
puts "Below is the output when amethod is called as ---> str = amethod(4) { |num| puts num }"
str = amethod(4) { |num| puts num}
puts "str = amethod(4) { |num| puts num } returned ---> #{str}"
puts "====================="
puts "Below is the output when amethod is called as ---> str1 = amethod { |num| puts num } ; i.e no argument to amethod."
str1 = amethod { |num| puts num }
puts "str1 = amethod { |num| puts num } returned ---> #{str1}"
puts "====================="
puts "Below is the output when amethod is called as ---> amethod { puts \"This output is from code block that does not have block arg.\" } ; i.e no argument to amethod, no code block arg and return value discarded."
amethod { puts "This output is from code block that does not have block arg." }
puts "====================="
puts "Below is the output when amethod is called as ---> amethod(&p); where p = Proc.new { puts \"I am a proc object\"}"
amethod(&p)
puts "===================="
puts "Below is the output when amethod is called as ---> ret = amethod(20, &p); where p = Proc.new { puts \"I am a proc object\"}"
ret=amethod(20, &p)
puts "ret = amethod(20, &p) returned: #{ret}"
puts "===================="
puts "Below is the output when bmethod is called as ---> bmethod { puts \"This output is from code block that does not have block arg.\" }"
bmethod { puts "This output is from code block that does not have block arg." }
puts "==================="
puts "Below is the output when bmethod is called as ---> bmethod(&p); where p = Proc.new { puts \"I am a proc object\"}"
bmethod(&p)
puts "==================="
puts "below is the output when cmethod is called as ---> cmethod(p); where p = Proc.new { puts \"I am a proc object\"}"
cmethod(p)
Following is the output of above code:
Below is the output when amethod is called as ---> str = amethod(4) { |num| puts num }
2
str = amethod(4) { |num| puts num } returned ---> 12
=====================
Below is the output when amethod is called as ---> str1 = amethod { |num| puts num } ; i.e no argument to amethod.
2
str1 = amethod { |num| puts num } returned ---> 30
=====================
Below is the output when amethod is called as ---> amethod { puts "This output is from code block that does not have block arg." } ; i.e no argument to amethod, no code block arg and return value discarded.
This output is from code block that does not have block arg.
=====================
Below is the output when amethod is called as ---> amethod(&p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
====================
Below is the output when amethod is called as ---> ret = amethod(20, &p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
ret = amethod(20, &p) returned: 60
====================
Below is the output when bmethod is called as ---> bmethod { puts "This output is from code block that does not have block arg." }
This output is from code block that does not have block arg.
===================
Below is the output when bmethod is called as ---> bmethod(&p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
===================
below is the output when cmethod is called as ---> cmethod(p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
No comments:
Post a Comment