A
Andrew Wagner
[Note: parts of this message were removed to make it a legal post.]
All,
I've been playing with some code to make querying an array more readable.
Basically, it lets you write one of the following:
puts [1,2,3].first that {|x| x.even? }
puts [1,2,3].first that is :even
instead of this:
puts [1,2,3].find {|x| x.even? }
The code is below. I'm curious whether you'd find it useful, interesting,
worthwhile, etc., and if there are improvements you can think of. Either
way, it was a fun little exercise. I love the flexibility I get from writing
in ruby!
module Kernel
def that proc=nil, &block
p = proc.nil? ? block : proc
lambda { |x| x.instance_eval &p }
end
def is message
lambda { |x| x.send "#{message}?".to_sym }
end
end
class Array
alias
ld_first first
def first func=nil
return old_first if func.nil?
each do |x|
return x if func.call x
end
nil
end
end
All,
I've been playing with some code to make querying an array more readable.
Basically, it lets you write one of the following:
puts [1,2,3].first that {|x| x.even? }
puts [1,2,3].first that is :even
instead of this:
puts [1,2,3].find {|x| x.even? }
The code is below. I'm curious whether you'd find it useful, interesting,
worthwhile, etc., and if there are improvements you can think of. Either
way, it was a fun little exercise. I love the flexibility I get from writing
in ruby!
module Kernel
def that proc=nil, &block
p = proc.nil? ? block : proc
lambda { |x| x.instance_eval &p }
end
def is message
lambda { |x| x.send "#{message}?".to_sym }
end
end
class Array
alias
def first func=nil
return old_first if func.nil?
each do |x|
return x if func.call x
end
nil
end
end