It’s a fact that Ruby on Rails programmers don’t buy books! Really! Ruby and Rails are changing fast. Books getting outdated in just a couple of months. Buying books is just a waste of time and money for nothing. At the time the books come out, Ruby on Rails developers already read blogs/saw screencasts or even get hands dirty committing to the rails core source code. Books are just for students, not for RoR...
We can makes Git’s status, branch, and diff commands much more readable by ading the following to ~/.gitconfig: [color] branch = auto diff = auto status = auto [color “branch”] current = yellow reverse local = yellow remote = green [color “diff”] meta = yellow bold frag = magenta bold old = red bold new = green bold [color “status”] added = yellow changed = green untracked =...
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...
Rails 3 assumes that everything is NOT html safe (a change of opinion from Rails 2). Now, all strings are html escaped by default: <%= h some_string %> is now the same as <%= some_string %> To unescape the HTML (i.e you already know that the string is OK to render out), you need to mark it as html_safe or use keyword raw : <%= some_string.html_safe %> or <%=raw some_string...
In this post I present 21 different Ruby “tricks,” from those that most experienced developers use every day to the more obscure. Whatever your level, a refresh may help you the next time you encounter certain coding scenarios. Note to beginners: If you’re still learning Ruby, check out Beginning Ruby book. 1 – Extract regular expression matches quickly A typical way to extract data from text using a regular expression is to use the match method. There is a shortcut, however, that can take the pain out of the process: email = "Fred Bloggs <fred@bloggs.com>" email.match(/<(.*?)>/)[1] # => "fred@bloggs.com" email[/<(.*?)>/, 1] # => "fred@bloggs.com" email.match(/(x)/)[1] # => NoMethodError [:(] email[/(x)/, 1] # => nil email[/([bcd]).*?([fgh])/, 2] # => "g" Ultimately, using the String#[] approach is cleaner though it might seem more “magic” to you. It’s also possible to use it without including an extra argument if you just want to match the entire regular expression. For example: x = 'this is a test' x[/[aeiou].+?[aeiou]/] # => 'is i' In this example, we match the first example of “a vowel, some other characters, then another vowel.” 2 – Shortcut for Array#join It’s common knowledge that Array#*, when supplied with a number, multiplies the size of the array by using duplicate elements: [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3] It’s not well known, however, that when given a string as an argument Array#* does a join! %w{this is a test} * ", " # => "this, is, a, test" h = { :name => "Fred", :age => 77 } h.map { |i| i...
singapore website design,singapore web design services,website developer singapore,developer in singapore,app developer singapore,ruby on rails developer singapore,web design services singapore,mobile application developer singapore,mobile apps singapore,mobile game developer singapore,ios app development singapore,web designer singapore,graphic designer in singapore,singapore mobile application developer,web development company singapore,website design singapore,singapore app developer,singapore mobile app developer,design agency singapore,singapore web design,singapore web development,website designer singapore,ios developer singapore,web design singapore,mobile app developer singapore,mobile developer singapore,developers in singapore,app development singapore,web development singapore,android developer singapore,web design company singapore,design firms in singapore,web application singapore,website development singapore,mobile app development singapore,mobile application development singapore,mobile apps development singapore