Ruby Tips #365 – WOW! A Year of Ruby Tips

Today marks the end of a 365 day journey of writing one Ruby programming tip everyday. It has been a long journey to consider new and interesting content in the realm of a Ruby and Rails programmer. I have learned so much and I am very happy to share...

Ruby Tips #364 – Cool Console Counter

This tip comes by way of: @starvo. By using a counter in your ruby console script you can gain insight into long running actions for your application. @photos = Photo.all count = @photos.count @photos.each_with_index do |photo, idx| # ... do a bunch of processing puts "#{(100.0 * idx /...

Ruby Tips #363 – Eql? versus == with Numerics

These two operator in Ruby look similar but can have different results when comparing Numeric operators. 1.0 == 1 #=> true 1.eql? 1 #=> false As stated in the docs only override the ‘==’ if needed. 🙂

Ruby Tips #362 – Super and Parentheses

Most the time in Ruby parentheses are optional for parameters in method calls. Here is one case where you can expect the unexpected. class Course def track puts 'masters' end end class Elective < Course def track(type) super puts type end end'art') #=> ArgumentError: wrong number of arguments...

Ruby Tips #361 – Access Heroku psql without Gems

One of the things making your Heroku hosted database, certainly from a remote box, or a console difficult is the need to get an application running with the Heroku gems. This means also means your system admin team needs to be concerned with Gems too. Here are the steps...

Ruby Tips #360 – Bang Bang Boolean

Ruby provides syntax for a !! boolean. In this example let’s extend this to a class default for our Yummy class. class Yummy def initialize @dinner_defined = false end def some_fudz !!@dinner_defined end end y = puts y.some_fudz #=> false

Ruby Tips #359 – Root Node for ActiveModel Serializers

To override your root nodes for ActiveModel serializers without a global config use the following. This makes a consistent API for clients regardless of model but still leaves your code flexible. # Controller render json: @liked, root: 'data' # Returns a collection like so. { "data" [{}, {}, {}]...

Ruby Tips #358 – Pass a Block to Strucks

In this example we can see how to pass a block to a Struck for a default value. Course = :title, :major, :location do def initialize(*) super self.location ||= 'UCLA' end end course ="Machine Learning", 'Comp Sci') => # course ="Machine Learning", 'Comp Sci', "UCSD") =>...

Ruby Tips #357 – Handy Flash Messages

The following is a nice block I have been using for years with Rails for flash and notice messages. It needs updating from time to time yet a nice thing to keep on your notepad. × "flash_#{name}" %> 🙂

Pending: Ruby Tips #356 – Turn Redis Strings into Hashes

This cool action shows how we can take a list of key/values stored in a Redis node and format into a Ruby hash for operating on. string = redis.get('resource:38292') => "0,foo,1,bar,2,chicken" >> Hash[string.split(,).each_slice(2).to_a] => {"0"=>"foo", "1"=>"bar", "2"=>"chicken"} 🙂