Skip to content

Instantly share code, notes, and snippets.

View worker8's full-sized avatar
:octocat:
code is compiling...

Tan Jun Rong worker8

:octocat:
code is compiling...
View GitHub Profile
@worker8
worker8 / block.rb
Created October 16, 2012 06:34
block
class String
def perform
yield self
end
end
#=== block ====
block_string = "Hey there, "
block_string.perform do |n|
class String
def perform code
code.call self
end
end
#===proc: storing into a variable====
proc_string1 = "Hey there, "
proc_code = Proc.new do |n|
puts n + "from a proc storing into a variable!"
@worker8
worker8 / lambda.rb
Created October 16, 2012 06:46
lambda
class String
def perform code
code.call(self)
end
end
#===lambda method====
lambda_string = "Hey there, "
lambda_code = lambda do |n|
puts n + "from a lambda!"
@worker8
worker8 / method.rb
Created October 16, 2012 06:47
method
class String
def perform code
code.call(self)
end
end
#=== method ====
method_string = "Hey there, "
def func n
@worker8
worker8 / how they work.rb
Created October 16, 2012 06:57
how they work
def proc_return
Proc.new { return "proc1"}.call
return "proc2 I AM HERE!"
end
def lambda_return
lambda { return "lambda1" }.call
return "lambda2 I AM HERE!"
end
@worker8
worker8 / code-replacement.rb
Created October 16, 2012 06:59
code-replacement.rb
def proc_return
return "proc1"
return "proc2 I AM HERE!"
end
@worker8
worker8 / what-class.rb
Created October 16, 2012 07:02
what-class
def what_class(&code)
return code.class
end
def func
"nothing"
end
puts (what_class do end)
puts Proc.new {}.class
puts lambda{}.class
puts method(:func).class
@worker8
worker8 / block2.rb
Created October 16, 2012 07:06
block2.rb
def argument_check_correct(&code)
code.call("a1","a2")
end
def argument_check_wrong(&code)
code.call("a1")
end
argument_check_correct do |a1,a2|
puts "block: arguments received: #{a1}, #{a2.class}"
@worker8
worker8 / proc2.rb
Created October 16, 2012 07:07
proc2.rb
def argument_check_correct(code)
code.call("a1","a2")
end
def argument_check_wrong(code)
code.call("a1")
end
proc1 = Proc.new{|a1,a2| puts "proc: arguments received: #{a1}, #{a2.class}"}
argument_check_correct proc1
@worker8
worker8 / lambda2.rb
Created October 16, 2012 07:07
lambda2.rb
def argument_check_correct(code)
code.call("a1","a2")
end
def argument_check_wrong(code)
code.call("a1")
end
lambda1 = lambda {|a1,a2| puts "proc: arguments received: #{a1}, #{a2.class}"}
argument_check_correct lambda1