Skip to content

Instantly share code, notes, and snippets.

@camertron
camertron / envize.rb
Created November 14, 2016 19:51
Move config from a .yml file into the .env
require 'yaml'
require 'optparse'
require 'set'
$options = { keys: [] }
$executable_name = 'envize.rb'
OptionParser.new do |opts|
opts.banner = "Usage: #{$executable_name} [options]"
@camertron
camertron / ruby_learning_5_13_2015.md
Last active August 29, 2015 14:21
Learning team bi-weekly code snippet for 5/13/2015

Three Useful Ruby Methods you Didn't Know Existed, and What to do With Them Now You've Wised Up

Yep, it's time for some cool Ruby trickery.

String#force_encoding

At their core, Ruby strings are just sequences of bytes. How you or your computer chooses interpret and display those bytes can mean the difference between legible text and complete garbage. Consider UTF-8 text for example. We now have great Unicode support in Ruby (as of 1.9) via the String#encode method and its friends, but again, strings are really just bytes.

Telling Ruby your string is encoded in UTF-8 tells it how to print your string on the screen or in your web app. For example, let's say you have a string that contains Japanese text (says 'arigato') "ありがと". Furthermore, let's pretend Ruby thinks the string is encoded in ASCII, which doesn't support Japanese characters. If you tried to print your string in the terminal, you might see something like this: "\xE3\x81\x82\xE3\x82\x8A\xE3\x81\x8C\xE3\x81\xA8". What you're seeing

@camertron
camertron / file_scope.rb
Last active August 29, 2015 14:19
Provides a way to perform Ruby `require` statements at the file (really the block) level
require 'find'
def provide(*requires, &block)
modules = requires.map do |req|
Module.new do
file_fragment = "#{req}.rb"
file = Find.find(*($: + ['.'])) do |f|
break f if f =~ /#{file_fragment}\z/
end
@camertron
camertron / ruby_learning_3_31_2015.md
Last active April 18, 2019 16:36
Concurrent programming in Ruby

Introduction

You may have heard before that Ruby isn't a concurrent language. In other words, it's not capable of doing two things at once. For the most part, you're right - the canonical version of Ruby that we use at Lumos (called MRI) locks the interpreter to a single thread of execution. This means you can never truly run a task "in the background" within the same process. MRI relaxes the lock however for certain IO operations, including file operations and network activity. The Ruby interpreter will "unlock" itself whenever it detects an IO operation, meaning other threads get to run. Pretty cool :)

Since Ruby does support at least some level of concurrency, it would be cool if we could use it in our apps. There are a couple of great Ruby concurrency libraries out there - namely Celluloid and concurrent-ruby. I'm going to talk about concurrent-ruby in this gist because it's the one I know best.

P

@camertron
camertron / convert_params.rb
Last active August 29, 2015 14:17
Converts params that appear in a Rails log to HTTP GET/POST params
require 'json'
require 'cgi'
def convert(data)
params = JSON.parse(data.gsub('=>', ':'))
[].tap do |out|
walk_hash(params, out, [])
end.join('&')
end
@camertron
camertron / ruby_learning_11_21_2014.md
Last active August 29, 2015 14:10
Lumos Labs Learning Team Bi-Weekly Ruby Snippet (11/21/2014)

One of the language features that really impressed me when I first started using Ruby was all the magic of Enumerable. No other language I know of lets you iterate so effectively over collections of objects. Let's say you wanted to sum together an array of integers in Java, for example:

int[] numbers = new int[5] { 5, 3, 8, 1, 2 };
int sum = 0;

for (int i = 0; i < numbers.length; i ++) {
  sum += numbers[i];
}
@camertron
camertron / number_transliterator.rb
Created October 30, 2014 19:36
Number Transliteration
require 'twitter_cldr'
class NumberTransliterator
class << self
def to_english_str(str)
str.each_char.map do |char|
transliterate_char(char) || char
end.join
end
@camertron
camertron / endpoint.rb
Last active August 29, 2015 14:08
Grape Middleware for Error Reporting
module Grape
class Endpoint
def build_middleware_with_error_tracking
builder = build_middleware_without_error_tracking
builder.use(::ErrorTrackingMiddleware)
# HAAAAACK!
builder.instance_variable_get(:'@use').tap do |use_arr|
middleware_proc = use_arr.delete_at(use_arr.size - 1)
use_arr.insert(0, middleware_proc)
@camertron
camertron / torquebox_error
Created September 22, 2014 19:15
Torquebox Error
12:12:11,975 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.967-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=JITCompiler
12:12:11,975 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.975-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=Config
12:12:11,976 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.976-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=ParserStats
12:12:11,976 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.976-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=ClassCache
12:12:11,976 ERROR [stderr] (ServerService Thread Pool -- 47) 2014-09-22T12:12:11.976-07:00: BeanManagerImpl: mbean already registered: org.jruby:type=Runtime,name=0,service=Runtime
12:12:13,237 ERROR [org.torquebox.core.runtime] (ServerService Thread Pool -- 47) Error during execution
@camertron
camertron / rbnf2.rb
Created January 6, 2014 22:28
RBNF example with twitter-cldr-rb, v3.0 branch
require 'twitter_cldr'
num = 1234.localize(:en) # locale is optional, defaults to :en
# "one thousand two hundred thirty-four"
num.spellout
# ["SpelloutRules", "OrdinalRules"]
num.rbnf.group_names