ファイルをopenしたときにエラーが起きると例外が起きます。 begin open("xxxx", "r") {|f| while line = f.gets() print ">> #{line}" end } rescue print "#{$!}\n" endファイルxxxxがない場合の実行結果です。 No such file or directory - xxxxファイルx…
メソッド呼び出しで引数に*をつけると、その内容が展開されます。 def rubyco(a, b, c, d) print "a = #{a}, b = #{b}, c = #{c}, d = #{d}\n" end a = [100, 200, 300] rubyco(0, *a) # => a = 0, b = 100, c = 200, d = 300
呼び出し側のアスタリスクは配列を展開しますが、受け取り側のアスタリスクは残りの引数を配列化します。 def rubyco(a, b, c, *d) p a p b p c p d end a = [100, 200, 300] rubyco(0, 1, *a)実行結果です。0, 1, 100, 200, 300が渡されるので、最後の200, …
yieldを使ってブロックを呼び出すと、自分でeachを作ることもできます。yieldへの引数がブロックパラメータとして渡されます。 class Array def my_each for e in self yield(e) end end end def print_them(a) a.my_each {|k| print "#{k} (#{k.class}), " …
proc { ... }で手続きオブジェクト(Procオブジェクト)を作ると、ブロック付きのメソッド呼び出しで手続きを別途渡すことができます。 class Array def my_each for e in self yield(e) end end end def print_them(a) myproc = proc {|k| print "#{k} (#{k.c…
to_procメソッドがあるオブジェクトならば、手続きオブジェクトとしてメソッドに渡すことができます。to_procメソッドはProcオブジェクトを返します。 class Rubyco def to_proc return proc {|k| print "#{k} (#{k.class}), " } end end class Array def my…
Rubyのif文は値を持ちますから、三項演算子と等価になります。 def abs1(n) if n > 0 return n else return -n end end def abs2(n) return n > 0 ? n : -n end def abs3(n) x = if n > 0 n else -n end return x end p abs1(-123) # => 123 p abs2(-123) # …
Rationalクラスで有理数が扱えます。Rational(分子, 分母)で有理数。分母を省略すると整数になります。 require "rational" def harmony(n) sum = Rational(0) for k in 1..n sum += Rational(1, k) end return sum end for n in 1..20 r = harmony(n) print…
Rubyco::Rosemaryは定数参照とみなされますが、Rubyco.Rosemaryはメソッド呼び出しとみなされます。 class Rubyco Rosemary = 123 def self.Rosemary return 456 end end p Rubyco::Rosemary # => 123 p Rubyco.Rosemary # => 456 p Rubyco::Rosemary() # =>…
Rubyのsuperは元メソッドを呼び出しますが、括弧と引数がないときには元メソッドの引数がそのまま渡されます。 class Parent def rubyco(x = 123, y = 456) print "Parent: rubyco(#{x}, #{y})\n" end end class Child < Parent def rubyco(x, y) if x == 0 …
Rubyでは、配列の参照はというメソッド、代入は=というメソッドになります。 class RubycoArray def initialize(size) @arr = [] @size = size end def []=(index, value) if index < 0 or @size <= index raise "index is out of bounds" else @arr[index] …
クラスメソッドnewを(superを呼ばずに)オーバーライドしてしまうと、newできなくなります。 class Rubyco def self.new print "new is overridden.\n" end def hello print "Hello!\n" end end a = Rubyco.new p a # => nil a.hello # => undefined method `…
クラスメソッドnewの最後にsuperじゃなく、他のオブジェクトを作ってみました。動きますね。 class Rosemary def hello print "Hello, Rosemary!\n" end end class Rubyco def self.new print "new is overridden.\n" Rosemary.new end def hello print "Hel…
でも、もちろん無理やりモジュールメソッドとしてnewを作ることはできますけれどね。 class RubycoClass def hello print "Hello!\n" end end module RubycoModule def self.new return RubycoClass.new end end a = RubycoModule.new a.hello # => Hello!
Rubyではincludeメソッドを使って、定義をインクルードできます。ということは、モジュールというのは抽象クラスのようなものかしら。 module RubycoModule def hello print "Hello!\n" end end class RubycoClass include RubycoModule end a = RubycoClass…
Rubyのモジュールをincludeすると、実質的に多重継承ができます。 module DebugPrint def debug_print p self end end class Rubyco include DebugPrint def initialize(name) @name = name end end class Diamond include DebugPrint def initialize(x, y, …