Last active
December 15, 2015 21:38
-
-
Save ilake/5326639 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
| # DATA, __END__, pos | |
| # http://shifteleven.com/articles/2009/02/09/useless-ruby-tricks-data-and-__end__ | |
| # http://stackoverflow.com/questions/1333720/ruby-scope-of-data-after-end | |
| # You could lambda in when | |
| case n | |
| when lambda(&:even?) | |
| puts "This number is even." | |
| ... | |
| # assoc | |
| s1 = [ "colors", "red", "blue", "green" ] | |
| s2 = [ "letters", "a", "b", "c" ] | |
| s3 = "foo" | |
| a = [ s1, s2, s3 ] | |
| a.assoc("letters") #=> [ "letters", "a", "b", "c" ] | |
| a.assoc("foo") #=> nil | |
| # rindex | |
| a = [ "a", "b", "b", "b", "c" ] | |
| a.rindex("b") #=> 3 | |
| a.rindex("z") #=> nil | |
| a.rindex{|x|x=="b"} #=> 3 | |
| # partition | |
| (1..6).partition {|v| v.even? } #=> [[2, 4, 6], [1, 3, 5]] | |
| # flat_map | |
| [[1,2],[3,4]].flat_map {|i| i } #=> [1, 2, 3, 4] | |
| # each_with_object | |
| %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } | |
| # => {'foo' => 'FOO', 'bar' => 'BAR'} | |
| # drop | |
| a = [1, 2, 3, 4, 5, 0] | |
| a.drop(3) #=> [4, 5, 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment