Created
December 5, 2012 20:18
-
-
Save sheenobu/4219136 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #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 |
This file contains hidden or 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
| [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