LoginSignup
9
9

More than 5 years have passed since last update.

Rubyアソシエーションビジネスセミナー試験案内講演資料(Ruby技術者認定試験Ruby1.8→Ruby 2.1の変更点まとめ)

Last updated at Posted at 2015-01-17

Rubyアソシエーションビジネスセミナー試験案内講演資料(2014/10/8)
内のサンプルコード。
コピペするとコードがずれたりしたのでまとめました。
自分の参照用のメモです。

実行結果が書かれていないコードは実行結果を追記しました。

Ruby 1.8 → Ruby 2.1 の変更点

  • 多言語化
  • リテラル
  • キーワード引数
  • 新しいラムダ式
  • Enumerable#lazy
  • Fiber
  • Module#prepend
  • Refinements

多言語化

# encoding: utf-8
p "あ".encoding.name 
#=> "UTF-8"

p "あいうえお"[0] 
#=> "あ" 

p ?
#=> "あ" 

p ?.ord
#=> 12354

p 97.chr
#=> "a" 

p 12354.chr("utf-8") 
#=> "あ"

リテラル

p({a: 1, b: 2})
#=> {:a=>1, :b=>2}

p 1/2r
#=> (1/2)

p 1i
#=> 0 + 1i

キーワード引数

def foo(x: 1, y: 2, **z)
  p x #=> 3
  p y #=> 2
  p z #=> {:a=>4, :b=>5}
end
foo(x: 3, a: 4, b: 5)

出力
3
2
{:a=>4, :b=>5}

新しいラムダ式

add = -> (x, y) {
  x + y
}
p add.call(1, 2) 
#=> 3

Enumerable#lazy

p (1..Float::INFINITY).lazy.map { |i|  
  i ** 2 
}.take(3).force
#=> [1, 4, 9]

Fiber

f = Fiber.new {
  x = 0
  loop do
    Fiber.yield(x)
    x += 1
  end
}
3.times do
  p f.resume
end
出力
0
1
2

補足

pry では実行できませんでした。

pry(main)> f = Fiber.new {
Error: Cannot find local context. Did you use `binding.pry`?

Module#prepend

module M
  def foo
    puts "M#foo"
  end
end

class C
  prepend M

  def foo
    puts "C#foo"
  end
end

C.new.foo 
#=> M#foo

補足

継承関係としては、prependしたモジュールMがクラスCより前に来る。

p C.ancestors
#=>[M, C, Object, Kernel, BasicObject]

Refinements

# Refinements
module M
  refine Fixnum do
    def /(other)
      self.quo(other)
    end
  end
end

p 1 / 2 
#=> 0

using M

p 1 / 2 
#=> (1/2)

補足

pry では using が有効でなかった。


pry(main)> # Refinements
pry(main)> module M
pry(main)*   refine Fixnum do
pry(main)*     def /(other)
pry(main)*       self.quo(other)
pry(main)*     end
pry(main)*   end
pry(main)* end
=> #<refinement:Fixnum@M>

pry(main)> p 1 / 2
0
=> 0

pry(main)> using M
=> main

pry(main)> p 1 / 2
0
=> 0

試験問題の例

以下のコードの実行結果として正しいものを選択してください

p ?x
選択肢A
?x
選択肢B
"x"
選択肢C
120
選択肢D
文法エラーが発生する

正解

選択肢B

参考リンク

Rubyアソシエーション: Ruby技術者認定試験制度

Ruby 2.1.0 リリース (2.0 からの変更点)

instance method Numeric#quo

quo(other) -> Rational | Float | Complex

self を other で割った商を返します。 整商を得たい場合は Numeric#div を使ってください。
Numeric#fdiv が結果を Float で返すメソッドなのに対して quo はなるべく正確な数値を返すことを意図しています。 具体的には有理数の範囲に収まる計算では Rational の値を返します。 Float や Complex が関わるときはそれらのクラスになります。
Numeric のサブクラスは、このメソッドを適切に再定義しなければなりません。
[PARAM] other:
自身を割る数を指定します。

1.quo(3)      #=> (1/3)
1.0.quo(3)    #=> 0.3333333333333333
1.quo(3.0)    #=> 0.3333333333333333
1.quo(0.5)    #=> 2.0

Complex(1, 1).quo(1)  #=> ((1/1)+(1/1)*i)
1.quo(Complex(1, 1))  #=> ((1/2)-(1/2)*i)

[SEE_ALSO] Numeric#fdiv

instance method Numeric#div

div(other) -> Integer

self を other で割った整数の商 q を返します。
.....
商に対応する余りは Numeric#modulo で求められます。 div はメソッド / を呼びだし、floorを取ることで計算されます。
メソッド / の定義はサブクラスごとの定義を用います。
[PARAM] other:
自身を割る数を指定します。

p 3.div(2) # => 1
p (-3).div(2) # => -2
p (-3.0).div(2) # => -2

instance method Fixnum#/

self / other -> Fixnum | Bignum | Float
div(other) -> Fixnum | Bignum | Float

算術演算子。商を計算します。
[PARAM] other:
二項演算の右側の引数(対象)
[RETURN]
計算結果

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9