Skip to content

Instantly share code, notes, and snippets.

View ohnishiakira's full-sized avatar

Akira Ohnishi ohnishiakira

View GitHub Profile
@ohnishiakira
ohnishiakira / Procfile
Created June 25, 2013 13:44
こういうProcfileを作ってforeman startすると、helloが死ぬとwebも殺されてしまってかなしい思いをする
web: bundle exec rackup config.ru -p $PORT
hello: echo "Hello, world"
@ohnishiakira
ohnishiakira / gist:5353172
Last active December 16, 2015 01:09
$;の値を変えるとString#splitの第1引数にnilを指定した時の挙動が変わる
#!/usr/bin/env rvm 2.0.0 do ruby
# coding: utf-8
p $;
#=> nil
p 'hoge fuga homu '.split(nil)
#=> ["hoge", "fuga", "homu"]
p $; = /\t/
#!/usr/bin/env ruby
# coding: utf-8
class Fixnum
define_method "+-" do |other|
self-other .. self+other
end
end
p 3.send("+-", 3)
@ohnishiakira
ohnishiakira / gist:5151391
Last active December 14, 2015 21:28
定数のリセット
# coding: utf-8
class Hoge
module DefaultConstants
FUGA = 3
HOMU = 4
end
include DefaultConstants
@ohnishiakira
ohnishiakira / gist:5149664
Created March 13, 2013 05:49
Ruby2.0のキーワード引数
#!/usr/bin/env rvm 2.0.0 do ruby
def hoge(h, o)
p [h, o]
end
def fuga(h: 1, o: 1)
p [h, o]
end
#!/usr/bin/env ruby
# coding: utf-8
def parse_attr(params)
Hash[params.map{|_|_.split(":").zip([:to_sym, :to_s]).map{|_,__|[_,__].inject(:send)}}]
end
params = ['rake:app', 'hoge:fuga']
p parse_attr(params)
#!/usr/bin/env ruby
# coding: utf-8
class Object
def idempotent
self
end
end
str = "hoge"
@ohnishiakira
ohnishiakira / gist:5062191
Created March 1, 2013 03:07
to_sの中でinspectするとSystemStackErrorで落ちる
class Hoge
def to_s
inspect
end
end
p Hoge.new
#=> stack level too deep (SystemStackError)
@ohnishiakira
ohnishiakira / gist:5046474
Created February 27, 2013 09:03
NokogiriでXMLを生成する
#!/usr/bin/env ruby
# coding: utf-8
require "nokogiri"
document = Nokogiri::XML::Builder.new do |doc|
doc.RootNode {
doc.ChildElement.hoge.fuga!.pero "Text in ChildElement"
doc.SecondChildElement(class: 'hoge', id: 'fuga')
}
@ohnishiakira
ohnishiakira / gist:5012159
Last active December 14, 2015 02:18
String#invert
#!/usr/bin/env ruby
# coding: utf-8
class String
def invert
return self unless self.split(//).uniq.length == 2
str = self
a, b = str.split(//).uniq
tmp = ([*"a".."z"] - [a, b]).first