Bootstrapped Thoughts

  • About
  • tags

Parallelising ETL workflows with the Jongleur gem

January 6, 2019

ETL with Jongleur
Continue reading

Nim for the discerning Rubyist

July 9, 2017

Nim and Ruby
Continue reading

RVM, gemsets and bundler

October 7, 2016

I use RVM with gemsets and Bundler to manage my Ruby environment and dependencies. Some people question that. “Isn’t RVM over-complicated?” they say. Or, “why are you using gemsets when you have bundler?” There are even some ethical objections: “I don’t like how RVM overrides shell commands!”
Allow me to address these questions, starting with the easy one: RVM overrides shell commands, mainly the cd command so that it can detect environment files when we change directories. So what!? It just works and unless you’re some kind of Shell Puritan that’s all you should care about. Besides, many other apps and utilities use all kinds of shims and proxies and no-one bats an eyelid. Get over it!

Continue reading

a quick word on Ruby proc objects

April 13, 2016

A couple of my Python colleagues tried to impress me today with Python's named slices feature. The way it works is like that:
 s = list('helloworld!')
  => ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '!']
WORLD = slice(5, 10)
s[WORLD]
 => ['w', 'o', 'r', 'l', 'd']
So you can have your own customized slicing mechanism that you can apply to any list. Which is kind of cool. That prompted me to demonstrate how we can do the same thing with Ruby. Luckily, in Ruby world we have blocks, procs and lambdas, the ultimate play-dough that allows us to stretch and flex in every direction.
​s = ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '!']
  => ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '!']
world = proc {|x | x.slice(5, 10)}
world[s]
 => ["w", "o", "r", "l", "d", "!"]

​so we can do things like:

first = proc { |x| x.slice(0) }
first[s]
=> "h"

or even things that we can't do with Python's named slicing, since it doesn't allow us to pass the receiver as an argument to the block (x in our case)

last = proc {|x| x.slice x.length-1, x.length}
last[%w(dog rabbit fox cat)]
=> ["cat"]

or

median = proc {|x| x.slice(x.length / 2) }
median[%w(dog rabbit cat)]
=> "rabbit"

and of course we're not just restricted to slicing arrays,

domain_extractor = proc { |x| x.gsub(/.+@([^.]+).+/, '\1') }
domain_extractor["joe@bootstrap.co.uk"]
=> "bootstrap"
​
and since it's a proc,  we can use it with any method that accepts procs and blocks
​

email_list = ["fred@bootstrap.me.uk", "john@gmail.com", "mary@yahoo.co.uk"]
=> ["joe@bootstrap.co.uk", "john@gmail.com", "mary@yahoo.co.uk"]
email_list.map(&domain_extractor)
=> ["bootstrap", "gmail", "yahoo"]

Proc objects can be wonderful for some quick , reusable functionality or for when defining a method would be too much, as well as for more serious uses such as callbacks. If you use them regularly, l'd love to know your use-cases and hints & tips! 
Continue reading

Scheduled Ruby jobs with Cron, RVM and Bundler 

November 25, 2015

Most of us use Resque and the Resque or Rufus schedulers to schedule tasks in an effective, controlled manner. However, we sometimes need to schedule jobs on machines that don’t have a queue installed. We then have to fall back on good ol’ raw Cron scheduling. The problem here is that Cron jobs usually run under the root user’s environment, which means that the RVM environment variables necessary for running our Ruby script aren’t being loaded, so the Cron job will fail.  

Continue reading
Next

Powered by Jekyll with Type Theme