Skip to content

Instantly share code, notes, and snippets.

@nz
Created March 31, 2010 18:32
Show Gist options
  • Save nz/350693 to your computer and use it in GitHub Desktop.
Save nz/350693 to your computer and use it in GitHub Desktop.
# blockarg.rb
def sequence2(n, m, c)
p("Inside sequence2")
i = 0
while(i < n)
yield i*m + c
i += 1
end
end
def sequence2a(n, m, c, &p)
p("Inside sequence2a")
p(p.class)
i = 0
while(i < n)
yield i*m + c
i += 1
end
end
def sequence3(n, m, c, &b)
p("Inside sequence3")
p(b.class)
i = 0
while(i < n)
b.call i*m + c
i += 1
end
end
def sequence4(n, m, c, b)
p("Inside sequence4")
i = 0
while(i < n)
b.call i*m + c
i += 1
end
end
sequence2(5, 2, 2){|x| puts x}
proc1 = Proc.new {|x| puts x}
puts
sequence2(5, 2, 2, &proc1)
puts
sequence2a(5, 2, 2){|x| puts x}
puts
sequence2a(5, 2, 2, &proc1)
puts
sequence3(5, 2, 2){|x| puts x}
puts
sequence3(5, 2, 2, &proc1)
puts
eltime2 = Time.now.usec
sequence4(6,3,3,proc1)
eltime3 = Time.now.usec
puts("sequence4 elapsed time: #{eltime3 - eltime2}")
@nz
Copy link
Author

nz commented Mar 31, 2010

Simplified from https://gist.github.com/350693/e8a61cb1b7c65fd5438c323da23bab4ff2a810a9

Replaced Time.now.usec with ruby benchmark module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment