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

Ruby Tips #344 – Duplicate Object Removal in Javascript and Ruby

We are faced with duplicate data in arrays quite often in programming. That data is not always simple numbers and “strings”. When we are faced with comparing objects things get a little more complex. Let’s look examples of stripping duplicate objects in both Javascript and Ruby. 1) Ruby example...

Ruby Tips #343 – Rails Multisites in One Application

By using request.host we can create a simple multisite application with CNAMES. This example would not scale very well but it works for creating a few statics sites with different domains. 1) Setup a controller and a basic route. # config/routes.rb Softwarehabit::Application.routes.draw do root 'sites#index' end # app/controllers/sites_controller.rb class...

Ruby Tips #342 – Ruby Chat Server, Part 3

In the first two examples of this chat server we looked as how to store connection in a class instance. Now let’s look at how the eventing will work. Part 1 Part 2 module Eventable def _callbacks @_callbacks ||= Hash.new { |k, v| k[v] = } end def...

Ruby Tips #341 – Get an Array of String Tokens in Ruby

In this example we look at how to match and get an array of Match object tokens from a string. It’s a great example of Ruby’s powerful String and Regexp methods. string = "crucial text more stuff more crucial stuff" matches = string.to_enum(:scan, /\(.*?)\/).map { Regexp.last_match } #=> [,...

Ruby Tips #340 – Get Column Types in ActiveRecord

ActiveRecord’s columns is a handy to abstract mixins. module Votable extend ActiveSupport::Concerns included do after_commit :all_the_things end def all_the_things self.columns_hash.each {|k,v| puts "#{v.type}"} end end #=> integer #=> string #=> text #=> datetime #=> string

Ruby Tips #339 – Simple Ruby Chat Server (Part 2)

Yesterday we looked at simple web server to serve as our chat server. Today let’s work towards the first class. Adding clients to the pool. First we update the TCPServer to accept multiple connections with a Thread.start. require 'socket' server = TCPServer.new 2000 loop do Thread.start(server.accept) do |connection| conn.puts...