Skip to content

Instantly share code, notes, and snippets.

@camertron
camertron / tiles.rb
Last active December 22, 2015 00:58
Dashboard tiles
class DashboardTile
PARAMS = [
:image, :name, :description, :count,
:count_suffix, :status, :url, :url_class
]
PARAMS.each do |param|
define_method(param) { raise NotImplementedError }
define_method(:"#{param}=") { raise NotImplementedError }
end
@camertron
camertron / rbnf_example.rb
Last active December 23, 2015 18:19
Rule-based Number Formatting with twitter-cldr-rb
# clone twitter-cldr-rb, change to rbnf branch
# Note: There are probably bugs with this implementation, and the function names will
# likely change (underscores instead of camel case, for example). Use only with the
# understanding that you will probably have to change your code later.
#
# Any and all questions/comments always appreciated!
require 'twitter_cldr/formatters/numbers/rbnf/en'
puts TwitterCldr::Formatters::RuleBasedNumberFormatter::English.renderDigitsOrdinal(123)
@camertron
camertron / clone_all_cskit.sh
Created December 14, 2013 04:21
Clone all cskit repos
#! /bin/bash
git clone [email protected]:CSOBerkeley/cskit-rb.git
git clone [email protected]:CSOBerkeley/cskit-shkts-rb.git
git clone [email protected]:CSOBerkeley/cskit-biblekjv-rb.git
git clone [email protected]:CSOBerkeley/cskit-hymnal-rb.git
@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
@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 / 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 / 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 / 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 / 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_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