Skip to content

patmccler/pat_and_aaron_first_git_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

My 4 Favorite Ruby methods

.sort

Sorts data given a comparison block. Non destructive!

["andrew", "pat", "garrett"].sort{ |x,y|  x.length <=> y.length } # ["pat", "andrew", "garrett"]

.inject

Given an iterable object and a initial value, it will perform the action of a block on each element and return the aggregate value. Non destructive!

[1,2,3,4].inject(0){ |sum, x| sum + x } # 10

.bsearch

On an array, it applies a binary search given a condition in a block

[1,2,3,4,5,7,8,9].bsearch{ |x| x > 7 } # 8

.flatten

On an array, it will take any nested arrays and make it into a 1-d array

[[1,2, [3,[4,5,6],7]],8,9,10].flatten # [1,2,3,4,5,6,7,8,9,10]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •