Skip to content

Instantly share code, notes, and snippets.

View taisyo7333's full-sized avatar

Daisuke Inoue taisyo7333

View GitHub Profile
@taisyo7333
taisyo7333 / ruby_exam_001.rb
Created February 22, 2016 00:02
Ruby 悩んだこと # 1
true || y = 1
p y # => nil
@taisyo7333
taisyo7333 / Code4-11.rb
Created January 25, 2016 14:02
Ruby Class : override instance method.
# encoding: SJIS
class Hoge
def method_missing(m,*args)
p "called:" + m.to_s
super # call original exception
end
end
Hoge.new.no_method
@taisyo7333
taisyo7333 / Code4-10.rb
Created January 25, 2016 13:58
Ruby Class methods : alias , undef
# encoding: SJIS
class Hoge
def huga1; end
def huga2; end
alias :huga3 :huga1
undef :huga2
end
p Hoge.instance_methods(false)
@taisyo7333
taisyo7333 / Code4-7.rb
Created January 25, 2016 13:52
Ruby Class
# encoding: SJIS
class Foo
def initialize(a)
@a = a
end
def method1
@a
end
end
@taisyo7333
taisyo7333 / Code4-3.rb
Created January 25, 2016 11:43
Ruby Class式評価時にクラス定義の内部が評価される
# encoding: SJIS
p 1
class Hoge
p 2
end
p 3
@taisyo7333
taisyo7333 / Code3-166.rb
Created January 24, 2016 10:30
Ruby throw - catch
# encoding: utf-8
def bar
catch(:calc) do
throw :calc , 100
end
end
p bar
# => 100
@taisyo7333
taisyo7333 / Code3-165.rb
Created January 24, 2016 10:26
Ruby throw - catch
# encoding: SJIS
def foo
throw :exit
end
catch(:exit) {
foo
p 1 # Do not call
}
@taisyo7333
taisyo7333 / Code3-164.rb
Created January 24, 2016 10:20
Ruby Exception
# encoding: SJIS
begin
1/0
rescue
p 1
rescue ZeroDivisionError # Do not call
p 2
end
# => 1
@taisyo7333
taisyo7333 / Code3-163.rb
Created January 24, 2016 10:19
Ruby Exception [retry]
# encoding: SJIS
a = 0
begin
b = 1 / a
rescue ZeroDivisionError
a += 1
retry
ensure
p b
@taisyo7333
taisyo7333 / Code3-162.rb
Created January 24, 2016 09:33
Ruby Exception $!
# encoding: SJIS
begin
1/0
rescue ZeroDivisionError
p $!.class # => ZeroDivisionError
raise # => ZeroDivisionError
end