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 = Struct.new :title, :major, :location do def initialize(*) super self.location ||= 'UCLA' end end course = Course.new("Machine Learning", 'Comp Sci') => # course = Person.new("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"} 🙂

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...