Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created May 2, 2014 22:11
Show Gist options
  • Save v2e4lisp/dbdbf660e76dd6a5bda8 to your computer and use it in GitHub Desktop.
Save v2e4lisp/dbdbf660e76dd6a5bda8 to your computer and use it in GitHub Desktop.
bind `it' to block, as default.
class Array
class It
def initialize(it)
define_singleton_method(:it) { it }
end
end
def self.iter_with_it(*methods)
methods.each { |m|
old = "_#{m.to_s}".to_sym
alias_method old, m
define_method(m) {|&block|
if block and block.arity.zero?
send(old) {|val| It.new(val).instance_eval &block }
else
send(old, &block)
end
}
}
end
iter_with_it :each, :map, :map!, :keep_if, :take_while, :drop_while
end
# [1,2,3].map { it + 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment