Ruby Tips #355 – Jim Weirich

It is a day to reflect on the lives we lead as a dear Rubyist has passed away. Jim Weirich has left us. His presentations were empathetic, important, and informative. His attitude as an engineer is something that can inspire everyone to be a more conscience person. His code...

Ruby Tips #354 – Rails 4.1 Convert to Array Before using Map()

In Rails 4.1 ActiveRecord relation objects removes mutator methods. # Release note # Relation no longer has mutator methods like #map! and #delete_if. Convert to an Array by calling #to_a before using these methods. # Rails 4.0 >> User.all.order('id DESC').map {|u| u } => [#> User.all.order('id DESC').map {|u| u...

Ruby Tips #353 – Request Variant in Rails 4.1

This is one cool feature for the latest version Rails, partly because I really enjoy seeing the Rails community build on front-end technologies. The addition is the request variant. class CoursesController < ApplicationController before_action :check_device def index @courses = Course.all respond_to do |format| format.js { render @courses.full } format.html.phone...

Ruby Tips #351 – Use Enumerable’s each_slice() for Templating

Ruby’s each_slice() makes it easy to split rows of data. This is especially useful when formatting tables or rows of data in templates. The following will provide a three column layout repeating down the page.

Ruby Tips #350 – Rails 4 @font-face Implementation

Less than a year ago there was an example of implementing custom choices with @font-face tags in Rails 3.1+. Today let’s look at a Rails 4+ implementation. 1) Using sass-bootstrap my application.css.scss includes. @import "bootstrap"; @font-face { font-family: 'Mathlete-Skinny'; src: font-url('/assets/Mathlete-Skinny-webfont.eot'); src: font-url('/assets/Mathlete-Skinny-webfont.eot?#iefix') format('embedded-opentype'), font-url('/assets/Mathlete-Skinny-webfont.woff') format('woff'), font-url('/assets/Mathlete-Skinny-webfont.ttf') format('truetype'), font-url('/assets/Mathlete-Skinny-webfont.svg#mathleteskinny')...

Ruby Tips #349 – Remove Require_tree with Multiple Layouts

If a Rails application has many layouts the manifests cannot require_tree. An infinite loop will occur if they aren’t removed. # app/assets/javascripts/application.js //= require jquery //= require jquery_ujs //= require turbolinks //= require bootstrap //= require_tree # app/assets/javascripts/admin.js //= require jquery //= require jquery_ujs //= require turbolinks //= require...

Ruby Tips #348 – Array#delete_if has no !

Ruby array’s delete_if doesn’t have a corresponding delete_if! This means the array is changed in real time if used. 🙂

Ruby Tips #347 – Hash Assignment in Begin Blocks

When dealing with exceptional (AKA failing) APIs reset your hashes in the rescue block to prevent 500s in responses. def reassignment h = {} begin h['party'] = "party" raise "styrig" rescue h = {} end h end def no_reassignment h = {} begin h['party'] = "party" raise "styrig" rescue...

Ruby Tips #346 – Rails ActiveRecord Refactor

Here is an example of some code we can refactor. 1) A clunky version. def self.populate! sections = Course.pluck(:section) available_courses = sections.uniq! available_courses.each do |name| Track.create(name: name) end end 2) Let’s reduce some lines def self.populate! Course.pluck(:section).uniq.each do |name| Track.create(name: name) end end But… What if our uniq constraint...

Ruby Tips #345 – Logging Sidekiq Jobs

Sidekiq can be difficult to debug, because the workers run in different processes and STOUT and Pry tactics won’t give you an indication of what is happening. Use these loggers instead. class CacheUsers include Sidekiq::Worker def perform logger.info { "Wow, I can see this" } logger.debug { "#{hash.inspect}" }...