Skip to content

Instantly share code, notes, and snippets.

@jeffyip
jeffyip / syntax_check.rb
Created April 18, 2012 21:23
A Script to Check the Syntax of Ruby Files -- Based off of a git precommit hook
#!/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
hello world
@jeffyip
jeffyip / case.rb
Created October 14, 2012 18:32
Ruby 1.9 Upgrade Guide Examples
class Cocktail
attr_accessor :name
def initialize(name)
@name = name
end
def liquor
case self.name
when "journalist": "gin"
when "sazerac": "whiskey"
# 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 / rails_json_cookie_session_store.rb
Last active September 23, 2020 07:03
Monkey Patch Ruby on Rails' cookie based session store to use JSON as its serializer instead of Marshal
# 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)