> Technologies Strategy - From Idea to Mobile App RealityVinova Our team will brainstorm with you on where to begin, where to go, and how to get you there. Whether you have a spark of an idea or an existing app – we can help. Getting your mobile strategy right is what our unique services are all about. We’ll wrestle with business challenges, discover new opportunities that will help you define and refine your product ideas into mobile app reality.

Things you can do with a Ruby array in one line

Just wanna share with you guys some of the common ruby array functions that I used every day.

1. Summing elements: This is a fairly common task. We’ll use Ruby’s inject method to sum all the items in the array and then print out the sum:

my_array.inject(0){|sum,item| sum + item}

2. Double every item: This is a class of problem where we want to preform an operation on every element of the array. Again, this is fairly simple using Ruby’s map method. Think of performing a “mapping” from the first array to the second based on the function in the block. Keep in mind, this will return a new array and will NOT effect the original array. If we want to do a destructive map (change the initial array) we would use map!. This is a common convention in Ruby:

my_array.map{|item| item*2 }

3. Finding all items that meet your criteria: If you want to collect all the values in the array that meet some criteria, we can do this using the (duh) find_all method. Again, this will return an array. The code below finds all items that are multiple’s of three :

my_array.find_all{|item| item % 3 == 0 }

4. Combine techniques: Let’s now say we want to find the sume of all elements in our array that are multiples of 3. Ruby to the rescue! This is very simple because we can chain methods together gracefully in Ruby. Check it out:

my_array.find_all{|item| item % 3 == 0 }.inject(0){|sum,item| sum + item }

5. Sorting: We can sort items in an array quite easily. Below, I will show the standard sort and then a sort based on the negative value of the number. Both are so simple, my head just exploded:

my_array.sort
my_array.sort_by{|item| item*-1}

6. Randomly pick n elements from an array

my_array.sort_by{ rand }.slice(0…number)

7. Sort by 2 criteria (and more):

my_array.sort{|x,y| [y.field_1, y.field_2] <=> [x.field_1, x.field_2] }

8. group_by

if you are using ruby 1.8.7+ or with active_support (or facets, I think), you can use group_by

products.group_by {|prod| prod.category}

9. Bonus!: As a bonus for all of you lovely readers, I will show you how to filter an array of strings based on a pattern. Let’s say you have huge array of strings and you want to collect any string that looks like this: 555-555-5555. Assume that the 5′s could be any digit (we are looking for phone numbers). First, we create a regular expression that expresses this phone number jazz: /d{3}-d{3}-d{4}/ (I’m not going into regular expressions here, but you can google them if you want to know more). Now, we simply use the find_all method discussed earlier combined with Ruby’s slick =~ operator. This will tell us if there is our string matches the regular expression. So, without further ado, here is how you would filter an array to find strings that contain phone numbers:

my_array.find_all{|item| item =~ /d{3}-d{3}-d{4}/ }

Don’t be shy, check out everything you can do with Ruby’s Array

Thanks for reading!

Malcare WordPress Security

web design company singapore,web application singapore,web design services singapore,ruby on rails developer singapore,ios developer singapore,app developer singapore,mobile game developer singapore,graphic designer in singapore,android developer singapore,developers in singapore,website design singapore,mobile application developer singapore,mobile application development singapore,singapore web design,mobile developer singapore,mobile app development singapore,design agency singapore,web development company singapore,website development singapore,singapore mobile app developer,mobile apps development singapore,ios app development singapore,web designer singapore,web design singapore,design firms in singapore,singapore app developer,singapore web development,singapore website design,app development singapore,website designer singapore,singapore web design services,mobile app developer singapore,mobile apps singapore,website developer singapore,singapore mobile application developer,web development singapore,developer in singapore