Skip to content

Instantly share code, notes, and snippets.

View kaznum's full-sized avatar

Kazuya NUMATA kaznum

View GitHub Profile
@kaznum
kaznum / person.rb
Created May 3, 2011 14:57
FIX ME: Sample for metaprogramming: Dynamic and lazy definition of attributes and methods.
class Person
def initialize(args)
args.each do |key, value|
self.class.send(:attr_accessor, :"#{key}") unless instance_variables.include?(:"@#{key}")
instance_variable_set(:"@#{key}", value)
end
end
def self.undef_has_a_var_which_begins_with(var)
method_name = "has_a_#{var}_which_begins_with?"
@kaznum
kaznum / expect_sample_spec.rb
Created September 27, 2011 01:42
Which is better to write when use expect{} on rspec
# I'm wondering which is better
# ex1: in it ".." block
describe "#create" do
it "should create an item" do
expect { Item.create(:name => 'foo') }.to change{Item.count}.by(1)
end
end
@kaznum
kaznum / lazy_evaluation_for_expect_spec.rb
Created September 27, 2011 01:52
Is there more beautiful expression of lazy evaluation for expect{} and DRY?
# FIXME
# Is there more beautiful expression of lazy evaluation for expect{} and DRY?
#
describe ItemsController do
describe "POST 'create'" do
before do
@action = lambda { post :create, :name => 'foo' }
end
describe "response" do
@kaznum
kaznum / string_crypt.rb
Created September 27, 2011 08:54
Crypt string
module StringCtypt
class String
def crypt
return Digest::SHA1.hexdigest(self)
end
end
end
@kaznum
kaznum / random_string.rb
Created September 27, 2011 08:59
generate random string
module RandomString
# chars example is ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
def self.random_of(chars, length=10)
Array.new(length){chars[rand(chars.size)]}.join
end
end
@kaznum
kaznum / monodevelop_gtkrc
Created September 29, 2011 14:42
gtkrc for monodevelop2 (/Library/Frameworks/Mono.framework/Versions/Current/etc/gtk-2.0/gtkrc)
#
# changed font for Japanese environment
# Probably it is OK to add 'font_name = "..."' in 'style "theme-default"' block.
#
include "/Library/Frameworks/Mono.framework/Versions/Current/share/themes/Clearlooks/gtk-2.0/gtkrc"
#gtk-icon-theme-name = "OSX"
gtk-icon-theme-name = "Tango"
gtk_color_scheme = "fg_color:#222\nbg_color:#e6e6e6\nbase_color:#f9f9f9\ntext_color:#222\nselected_bg_color:#788ab0\nselected_fg_color:#fff"
gtk-menu-popup-delay = 1
gtk-button-images = 0
@kaznum
kaznum / integer_comma_separated.rb
Created September 29, 2011 15:03
return the comma separated string of integer.
class Integer
def to_comma_separated()
self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse
end
end
@kaznum
kaznum / mysql_index.sql
Created October 9, 2011 15:43
mysql table index
alter table foo_table add index(column1);
show indexes from foo_table;
@kaznum
kaznum / fix_too_slow_input_in_gnome_terminal.sh
Created October 14, 2011 12:47
Fix too slow input in gnome-terminal in Debian
#!/bin/bash
aptitude install ibus-gtk3
@kaznum
kaznum / httpd_passenger_subpath.conf
Created October 21, 2011 12:56
set up rails application to sub-path on passenger in apache.
# set up rails application to sub-path on passenger in apache.
# see more http://www.modrails.com/documentation/Users%20guide%20Apache.html#PassengerAppRoot
Alias /hoge /var/www/hoge_app/public
<Directory /var/www/hoge_app/public>
PassengerAppRoot /var/www/hoge_app
</Directory>