Created
November 20, 2013 22:50
-
-
Save alksl/7572614 to your computer and use it in GitHub Desktop.
Path for values_at to include block predicate
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 values_at(*args) | |
arr = [] | |
each_index do |i| | |
if args_include_index?(args, i) or (block_given? and yield(i)) | |
arr << self[i] | |
end | |
end | |
arr | |
end | |
private | |
def args_include_index?(args, i) | |
args.each do |arg| | |
if arg.respond_to?(:include?) | |
return true if arg.include?(i) | |
elsif arg.to_i == i | |
return true | |
end | |
end | |
false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment