Daily Ruby Tip #12 – Hashify International Phone Numbers

Sometimes you feel like a Brit, sometimes you don’t. This check should and will be more robust, but for now it’s an example in Array indexing mostly. #returns a hash with the number, country, and prefix def hashify_phone_number_country(number) return if number.blank? # strip everything but the numbers number =...

Daily Ruby Tip #11 – Test A Protected or Private Method in A Controller Test

As mention in Ruby writings, testing a protected or private method is not recommended, but sometimes you find code that needs a massive refactor and there is large protected method that surely needs to be tested. Here is one way to test such a method. context "on POST to...

Daily Ruby Tip #10 – Create A Whitelist of Admins

Setup a quick whitelist of admin users for your application. Why include extra gems when it’s so easy. *This assumes you have a current_user object available in your application. users = ["coco@brettu.com", "jb@brettu.com", "dillan@brettu.com"] def admin? users.include?(current_user.email) ? true : false end 🙂

Daily Ruby Tip #9 – The classic pallendrom recursion in Ruby (two methods)

Classic recursion in Ruby. 1. Use String indexes def palindrome?(string) if string.length == 1 || string.length == 0 true else if string[0] == string[-1] palindrome?(string[1..-2]) else false end end end string = "abcba" palindrome?(string) => true 2. Use Ruby String methods def palindrome?(string) str == str.reverse end string =...

Daily Ruby Tip #8 – Spaceships in Ruby

Sort memorized lists with the spaceship operator. >> list = Set["sushi", "Pizza", "pho"] => # >> list.sort => ["Pizza", "pho", "sushi"] >> list.sort {|a,b| ba} => ["sushi", "pho", "Pizza"] In the future I will post many most posts and examples of the . It returns a 1, 0, -1...

Daily Ruby Tip #7 – Pass an operator method to a collection with .reduce()

This Ruby feature is pretty cool. Previous versions of Ruby normally use .inject() and pass a block to achieve the same result reduce. sum = (1..5).inject {|total, x| total + x } => 15 sum = (1..5).reduce(:+) => 15

Daily Ruby Tip #6 – Pass “args” to a Ruby script

Say you need a one off Ruby script to preform some action and you don’t want to keep opening a console. You can pass arguments via a command line application like this: 1. Create a Ruby file. $ cd ~/Desktop $ vim pass_args.rb 2. Add the Ruby header, and...

Daily Ruby Tip #5 – Create your own map() or collect() method

A good Ruby programmer knows how to spin their own Ruby methods. Here is an example of what Ruby map() or collect() in the class Enumerable could look like. arr = %w{surf, sail, run} def my_map(array) i = 0 while(i < array.count) yield array[i] if block_given? i += 1...

Daily Ruby Tip #4 – Use map() inside a method to arrange arguments

This one is from the Ruby Metaprogramming book. Collapse a list of method arguments into an Array in Ruby. def organize_your_code(*args) args.map {|arg| arg } end >> organize_your_code(“spinach”, “margarita pizza”, “tiramisu”) => [“spinach”, “margarita pizza”, “tiramisu”] Happy Coding 🙂

Daily Ruby Tip #3 – Use YAML for a more readable console

Print console or IRB output in a more readable format without Pry or similar gems. $ rails console >> object = Object.new => # >> y object --- !ruby/object {} => nil With a large object you will see the object in a vertical column. 🙂