Skip to content

Instantly share code, notes, and snippets.

View kachick's full-sized avatar
😋
😪

Kenichi Kamiya kachick

😋
😪
View GitHub Profile
describe "Foo" do
context "Bar" do
it "Baz" do
end
end
end
#=> "Foo Bar Baz"
describe "Foo" do
@kachick
kachick / lookup_const.rb
Created September 2, 2013 11:31
定数の参照 - 前にruby-listへ投稿したけど特に反応が得られなかったもの http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/48791
$VERBOSE = true
class Super
class << self
CONS = :Singleton
end
CONS = :Super
end
@kachick
kachick / list.rst
Last active December 22, 2015 01:38
パーフェクトRuby(初版・第一刷)の正誤表が無いっぽいので、とりあえず流し読みして気づいた限りをまとめてみた。

My 正誤表 - パーフェクトRuby(初版・第一刷)

そのうち、 サポートページ とかに正式なの出来るのかもしれませんけど、2013/8/31時点では見当たら無かったので。

===== ===== Page こうじゃないかなーと思った部分 ===== ===== 136 is_a? -> instance_of?

@kachick
kachick / integer-pred.rb
Created August 15, 2013 05:51
Integer#pred from Haskell
class Integer
def pred
self - 1
end
end
1.pred #=> 0
0.pred #=> -1
@kachick
kachick / array-init.rb
Created August 15, 2013 03:43
Array#init from Haskell
class Array
def init
self[0..-2]
end
end
[1, 3, 7].init #=> [1, 3]
@kachick
kachick / 0.README.md
Last active December 20, 2015 22:29
Class.allocate.new crashed on rbx

rubinius 2.0.0.n224 (1.9.3 e91782d0 2013-08-12 JI) [x86_64-unknown-linux-gnu]

Class.allocate.allocate #=> pass
Class.allocate.new #=> crash
Class.allocate.allocate.send(:initialize) #=> crash
# find_longest_api_name
# 2013 Kenichi Kamiya
# ruby -v: ruby 2.1.0dev (2013-08-10 trunk 42476) [x86_64-linux]
$VERBOSE = true
class BasicObject
def longest_method_name
(methods(true) | private_methods(true)).reject { |name|
/\Alongest_(?:instance_)?method_name\z/ =~ name }.max_by(&:length)
@kachick
kachick / taint-object_model.rb
Last active December 20, 2015 20:59
taint with singleton_class, Class, Module
RUBY_DESCRIPTION #=> "ruby 2.1.0dev (2013-08-09 trunk 42473) [x86_64-linux]
$VERBOSE = true
obj = Object.new
mod = Module.new.taint
obj.taint.singleton_class.tainted? #=> true
obj2 = Object.new
@kachick
kachick / array-xor.rb
Created August 7, 2013 10:16
Array#^
# https://twitter.com/yuroyoro/status/365050923237638144
class Array
def ^(other)
(self | other) - (self & other)
end
end
[1, 2, 3] ^ [3, 4] #=> [1, 2, 4]
@kachick
kachick / multiple_yields_are_arrays.rb
Created July 28, 2013 14:46
yieldへ2つ渡しててもブロック無しで呼んだらただのArrayにしかならないから、真偽使ってるなら気をつけたほうがいいかもね的なアレ
# https://github.com/rubinius/rubinius/blob/7866fbd80db7fe6e1de2627f5442c8abe6ce51ad/kernel/common/enumerable.rb#L113
# https://github.com/rubinius/rubinius/blob/7866fbd80db7fe6e1de2627f5442c8abe6ce51ad/kernel/common/enumerable.rb#L428
class Foo
def each
yield false
yield nil
end
end