Daily Ruby Tip #22 – Navigate Your Position from Console

Rails console is a great way to execute scripts, but what if the script is not located on your folder dir or you need to move to a folder with different permissions. The Ruby Dir Class is perfect for moving around the file system to find what you need....

Daily Ruby Tip #21 – How to Create Directories in Ruby

Collecting media for all reasons is a common thing we do. I like to keep my directory structure clean and organized on my machine. Today I was faced with an issue of downloading a bunch of educational media, so I needed 100 folders to organize my downloads. Creating 100...

Daily Ruby Tip #20 – Use The Delegate Method in Rails to Reduce Code

Yesterday I exampled a quick setup of a class in Ruby. Today I will use the same class but in the Rails context. I will also use the ‘delegate’ method to reduce code. To see the original code click here. Here is the refactor in Ruby on Rails with...

Daily Ruby Tip #19 – A Basic Class Setup in Ruby

Here is a simple class setup in Ruby. This is how we might setup a user that belongs in a class in Ruby. There are other ways to reduce this code. Tomorrow’s tip I will include the ‘delegate’ method in Rails to show how to clean this up. class...

Daily Ruby Tip #18 – Quick Assignment in Ruby

A great feature of Ruby is the quick assignment of variables and methods. >> cheese, milk, eggs, yogurt = 'cow', 'goat', 'buffalo', 'llama' => ["cow", "goat", "buffalo", "lama"] >> cheese => "cow" >> milk => "goat" >> eggs => "buffalo" >> yogurt => "llama" def dairy_farm_animals(*args) cheese, milk, buffalo,...

Daily Ruby Tip #17 – Use “||” at The End of Loop

Method chaining is a great feature of object orientation. In this syntax example I show how to return a message if iterating over a block returns nil. >> (1..10).detect { |i| i % 5 == 0 and i % 7 == 0 } => nil >> (1..10).detect { |i|...

Daily Ruby Tip #16 – Add Procs to ActiveRecord Callbacks

Basic Proc.new usage in Rails before_validation :some_method, :unless => Proc.new {|model| model.active? } The callback is only called id the model responds truth to active?

Daily Ruby Tip #15 – Error Message Attribute Name Adjustment in Rails

The original error message in this code was: self.errors.add(:number, I18n.translate('activerecord.errors.messages.too_long', :count => 15)) This returned an error message that wasn’t quite effective enough, “Number is too short (minimum is 7 characters)”. Which number is the site talking about? Here is a tip tease the validator with the proper attribute...

Daily Ruby Tip #14 – Nil.try() and to_f()

Calling the methods on nil return the expected ‘nil’ method result as documented, but ‘trying’ a method returns nil. ➜ rails console >> nil.to_f => 0.0 >> nil.try(:to_f) => nil >> nil.to_s => "" >> nil.try(:to_s) => nil

Daily Ruby Tip #13 – Dealing with Ruby Nil class

The Ruby ‘nil’ class has a variety of methods. I like to consider a nil check in all my actions. Here are some examples: 1) return from the method early def common_method(data) return nil if data.nil? # do everything else you want to do end 2) Return if you...