Array Manipulation

This page is a tutorial for array manipulation using Ruby (Rails). To find a value on a collection of array:
  
  a = [1,2,3,4,5]
  a.include?(3)   # => true
  a.include?(9)   # => false
  
You can find more info regarding rails array here.

To add hashes into an array and sort it:
  
  array = Array.new
  array << {a: "zach", b: "z"}
  array << {a: "john", b: "j"}
  array << {a: "peter", b: "pet"}
  array.sort_by{ |hash| hash[:a] }
  

No comments:

Post a Comment