Skip to content

Instantly share code, notes, and snippets.

View additional_monkey_patches.rb
# encoding: utf-8
# code from: http://techtime.getharvest.com/blog/harvest-is-now-on-ruby-1-dot-9-3
# and also: https://gist.github.com/1976864
# Serialized columns in AR don't support UTF-8 well, so set the encoding on those
class ActiveRecord::Base
def unserialize_attribute_with_utf8(attr_name)
traverse = lambda do |object, block|
if object.kind_of?(Hash)
@jeffyip
jeffyip / rails_json_cookie_session_store.rb
Last active Sep 23, 2020
Monkey Patch Ruby on Rails' cookie based session store to use JSON as its serializer instead of Marshal
View rails_json_cookie_session_store.rb
View convert_string_encoding.rb
# Encodes a string from encoding "from" to encoding "to" in
# a way that works for both ruby 1.8 and 1.9
def convert_string_encoding(to, from, str)
if "1.9".respond_to?(:force_encoding)
str = str.dup if str.frozen?
str.encode(to, from, :undef => :replace)
else
require 'iconv'
Iconv.conv(to, from, str)
end
@jeffyip
jeffyip / case.rb
Created Oct 14, 2012
Ruby 1.9 Upgrade Guide Examples
View case.rb
class Cocktail
attr_accessor :name
def initialize(name)
@name = name
end
def liquor
case self.name
when "journalist": "gin"
when "sazerac": "whiskey"
@jeffyip
jeffyip / syntax_check.rb
Created Apr 18, 2012
A Script to Check the Syntax of Ruby Files -- Based off of a git precommit hook
View syntax_check.rb
#!/usr/bin/env ruby
require 'open3'
include Open3
skip_erb_files = true
compiler_ruby = `which rbx`.strip
compiler_ruby = `which ruby`.strip if compiler_ruby.length == 0