Skip to content

Instantly share code, notes, and snippets.

@rbreve
Created March 10, 2011 07:05
Show Gist options
  • Save rbreve/863685 to your computer and use it in GitHub Desktop.
Save rbreve/863685 to your computer and use it in GitHub Desktop.
An iterate array function with yield
class Array
def iterate!
self.each_with_index do |n, i|
self[i] = yield(n)
end
end
end
class Array
def iterate_proc!(&code)
self.each_with_index do |n, i|
self[i] = code.call(n)
end
end
end
class Array
def iterate_proc2!(code)
self.each_with_index do |n, i|
self[i] = code.call(n)
end
end
end
array_1 = [1, 2, 3, 4]
array_2 = [2, 3, 4, 5]
square = Proc.new do |n|
n ** 2
end
array.iterate_proc2(square)
def args(code)
one, two = 1, 2
code.call(one, two)
end
args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})
args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment