Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created December 5, 2012 20:18
Show Gist options
  • Select an option

  • Save sheenobu/4219136 to your computer and use it in GitHub Desktop.

Select an option

Save sheenobu/4219136 to your computer and use it in GitHub Desktop.
#convert a list of integers into a list of booleans.
1.9.3p194 :026 > [1,2,3,4].map { |x| x == 3 } => [false, false, true, false]
# return true if any integer is three
1.9.3p194 :027 > [1,2,3,4].map { |x| x == 3 }.reduce(false,:or) => true
# return true if ALL integers are three
1.9.3p194 :028 > [1,2,3,4].map { |x| x == 3 }.reduce(true,:and) => false
# return true if ALL integers are three
1.9.3p194 :031 > [3,3,3,3].map { |x| x == 3 }.reduce(true,:and) => true
# sum 1 through 20
1.9.3p194 :052 > (1..20).sum => 210
# sum 1 through 20
1.9.3p194 :055 > (1..20).reduce(0,:+) => 210
[TrueClass,FalseClass].each do |clazz|
clazz.class_eval do
def or(a)
self or a
end
def and(a)
self and a
end
end
end
require 'active_support/all'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment