Created
March 10, 2011 07:05
-
-
Save rbreve/863685 to your computer and use it in GitHub Desktop.
An iterate array function with yield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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