January 2010
2 posts
2 tags
New proc syntax in 1.9
BEGIN { puts "Ruby Version : #{RUBY_VERSION}" } succ = ->(x=0) { x + 1 } p succ.call p succ.call(2) successor = Proc.new {|x=0| x + 1} p successor.call p successor.call(10) Output Ruby Version : 1.9.1 1 3 1 11 But thanks, I will stick with the old syntax!
Jan 4th
1 note
2 tags
Fibers in Ruby 1.9
class Fib include Enumerable def initialize(stop_at) @count = 0 @stop_at = stop_at @first, @second = 0, 1 @fiber = Fiber.new do loop do @first, @second = @second, @first + @second @count += 1 Fiber.yield @first end end end def next @fiber.resume end def rewind @first, @second = 0, 1 end def each loop...
Jan 3rd