Skip to content

Instantly share code, notes, and snippets.

@muhammadyana
Last active June 8, 2017 04:07
Show Gist options
  • Save muhammadyana/c8812ab1deb6d43c565f6743dc5eccb4 to your computer and use it in GitHub Desktop.
Save muhammadyana/c8812ab1deb6d43c565f6743dc5eccb4 to your computer and use it in GitHub Desktop.
Block also can be an object in ruby
print "Enter parameter = "
var = gets()
class ProcExample
def pass_in_block(&action)
@stored_proc = action
end
def use_proc(parameter)
@stored_proc.call(parameter) #call the parameter
end
end
eg = ProcExample.new
eg.pass_in_block{
|param|
puts "the parameter is #{param}"
}
eg.use_proc(var)
def CreateBlockObjeck(&block)
block
end
#the simple way
create = CreateBlockObjeck{
|param|
puts "call me #{param}"
}
create.call var
#modifikasi
# class AnotherWay
# def CreateBlockObjeck(&block)
# block
# end
# end
#
# #the simple way
# cl = AnotherWay.new
# cl.CreateBlockObjeck{
# |param|
# puts "call me #{param}"
# }
#cl.call var
#way of three
way3 = lambda {
|param|
puts "Param is #{param}"
}
way3.call var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment