Created
March 10, 2010 16:04
-
-
Save ilake/328007 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
| def get_vehicle_report(vehicle_id) | |
| vehicle = Vehicle.find(vehicle_id) | |
| returning Report.new(vehicle_id, vehicle.year, vehicle.description) do |report| | |
| if report.something.nil? | |
| owner = vehicle.owner | |
| # ... | |
| end | |
| end | |
| end | |
| ## group_by | |
| # - Partition a collection based on the value returned by a block | |
| >> (1..20).group_by {|n| n%3} | |
| # => {0=>[3,6,9,12,15,18], | |
| # 1=>[1,4,7,10,13,16,19], | |
| # 2=>[2,5,8,11,14,17,20]} | |
| ## index_by | |
| # - Convert a collection into a hash where keys are values returned by a block | |
| >> (1..5).index_by {|n| n*10} | |
| # => {50=>5, 40=>4, 30=>3, 20=>2, 10=>1} | |
| ## sum | |
| # - Sum the values returned by the block | |
| >> [1,3,5,7,9].sum | |
| # => 25 | |
| >> [1,3,5,7,9].sum {|n| n*n} | |
| # => 165 | |
| >> Orders.find(:all).sum {|o| o.total} | |
| # => 12345.67 | |
| ## in_groups_of | |
| # - Partitions an array into chunks of N elements | |
| >> %w{ a b c d e f g h i j }.in_groups_of(3) | |
| # => [["a", "b", "c"], | |
| # ["d", "e", "f"], | |
| # ["g", "h", "i"], | |
| # ["j", nil, nil]] | |
| ## to_sentence | |
| # - Like #join on steroids (sometimes useful for listing tags) | |
| >> names = %w{ Ryan Paul Heath } | |
| >> names.to_sentence | |
| # => "Ryan, Paul, and Heath" | |
| >> names.to_sentence(:connector => "and also") | |
| # => "Ryan, Paul, and also Heath" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment