Skip to content

Instantly share code, notes, and snippets.

@alksl
Created November 20, 2013 22:50
Show Gist options
  • Save alksl/7572614 to your computer and use it in GitHub Desktop.
Save alksl/7572614 to your computer and use it in GitHub Desktop.
Path for values_at to include block predicate
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