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

Ruby Tips #338 – A Simple Chat Server (Part 1)

This example is a simple web server in Ruby. Run the program and navigation your browser to http://localhost:2000. This is beginning of a series to build a chat server in Ruby. Something often regarded as not possible. require 'socket' server = TCPServer.new 2000 # Server bound to port 2000...

Ruby Tips #337 – Practicing Ruby goes Public

Today Gregory Brown opened up https://practicingruby.com/ to the public. I have been a subscriber of this resource a little over a year and it has been a great read for the weekends and gaining better understanding of less covered topic in Ruby (ie… not web). There are still some...

Ruby Tips #336 – Turn Rails into a Game Engine

There is great library out for game creation named Gosu. This example show how to turn your Rails application into a Gosu game in five minutes. 1) Create a Rails application, add the gem, bundle and add a window model, $ rails new game_engine # Gemfile gem 'gosu' #...

Ruby Tips #335 – Fallback to Yaml when ENVs aren’t present

To keep secure keys out of repositories we commonly use ENV variables in different environments. The following is an example of a Carrierwave configuration that will fallback to s3.yml when a user doesn’t have ENV variable credentials set for s3 file uploads. The same principle can be used for...