Posts

Showing posts from March, 2015

Ruby Array : sort, reverse and unique

numbers = [1, 4, 6, 7, 3, 2, 5] => [1, 4, 6, 7, 3, 2, 5] Sort numbers.sort => [1, 2, 3, 4, 5, 6, 7] numbers = [1, 4, 6, 7, 3, 2, 5] => [1, 4, 6, 7, 3, 2, 5] numbers.sort! => [1, 2, 3, 4, 5, 6, 7] Reverse numbers.reverse => [7, 6, 5, 4, 3, 2, 1] Unique numbers = [1, 4, 6, 7, 3, 2, 5, 1, 2] => [1, 4, 6, 7, 3, 2, 5, 1, 2] numbers.uniq => [1, 2, 3, 4, 5, 6, 7] numbers = [1, 4, 6, 7, 3, 2, 5] => [1, 4, 6, 7, 3, 2, 5] numbers.uniq! => [1, 2, 3, 4, 5, 6, 7] Note : Using Exclamation mark to make changes in original array

Ruby Array : Push and Pop

Push chars = ["a", "b", "c"] => ["a", "b", "c"] chars.push "d" => ["a", "b", "c", "d"] chars.push "e" => ["a", "b", "c", "d", "e"] Pop : Last In First Out (LIFO) chars => ["a", "b", "c", "d", "e"] chars.pop => "e" chars.pop => "d"

Ruby Array : Modification

chars = ["a", "b", "c"] => ["a", "b", "c"] Insertion chars.insert( 1, "d" ) => ["a", "d", "b", "c"] Modify one element chars = ["a", "b", "c"] chars[1] = "d" => "d" chars => ["a", "d", "b"] Modify multiple elements using range chars = ["a", "b", "c"] => ["a", "b", "c"] chars[1..2] = "d", "e" => ["d", "e"] chars => ["a", "d", "e"]

Ruby Array : Deleting Elements

Deleting Array Elements chars = ["a", "b", "c"] => ["a", "b", "c"] Approach 1 : Using index colors.delete_at(1) => "b" colors => ["a", "c"] Approach 2: Using the actual value colors = ["a", "b", "c"] => ["a", "b", "b"] colors.delete("b") => "b" colors => ["a", "c"]

Ruby Array : Comparison

Lets start with two arrays array1 = ["x", "y", "z"] array2 = ["w", "x", "y"] Tip 1: Add two arrays and remove duplicates array1 | array2 => ["x", "y", "z", "w"] Tip 2: Keeping common elements and remove duplicates array1 & array2 => ["x", "y"] Tip 3 : Keeping unique elements array1 - array2 => ["z"]

Ruby Array : Concatenation

Arrays Concatenation  Approach 1 chars1 = ["a", "b", "c"] chars2 = ["d", "e", "f", "g"] chars = chars1 + chars2 => ["a", "b", "c", "d", "e", "f", "g"] Approach 2 chars1 = ["a", "b", "c"] chars2 = ["d", "e", "f", "g"] days = chars1.concat(chars2) => ["a", "b", "c", "d", "e", "f", "g"] Approach 3 chars = ["a", "b", "c"] chars << "d" << "e" << "f" << "g" => ["a", "b", "c", "d", "e", "f", "g"]

Cherry pick in Git

Cherry picking in git is a way by which you can move a commit from one branch and apply it onto another. Make sure you are on the branch you want apply the commit to git checkout master Execute the following: git cherry-pick <commit-hash> commit-hash can be found using git log

Titanium constants

These are several useful constants in Alloy. OS_IOS True if the app is running on iOS OS_ANDROID True if the app is running on Android OS_MOBILEWEB True if the app is running in mobile web ENV_DEV True if the app is running in the simulator ENV_TEST True if the app is running on a device ENV_PRODUCTION True if the app has been built for distribution