LoginSignup
95
96

More than 5 years have passed since last update.

Ruby 2.2.0 の新規フィーチャーをネチっこくおさわりする #ruby

Last updated at Posted at 2014-12-26

Ruby 2.2.0 の新規フィーチャーを :kissing_closed_eyes::wave:ネチっこくおさわり:wave::neckbeard: する #ruby

概要

2014/12/25 にリリースされた Ruby 2.2.0 の新規フィーチャーを

:wave::wave::wave::wave::wave::neckbeard::kissing_closed_eyes::wave::wave::wave::wave:
:kissing_closed_eyes::wave: ネチっこくおさわり :wave::neckbeard:
:wave::wave::wave::wave::wave::kissing_closed_eyes::neckbeard::wave::wave::wave::wave:

してみます

:books: リリースノート

前提

:book: Binding

:page_with_curl: Binding#local_variables

pry で doc を確認

[6] pry(main):1> ? local_variables

From: vm_eval.c (C Method):
Owner: Kernel
Visibility: private
Signature: local_variables()
Number of lines: 7

Returns the names of the current local variables.

   fred = 1
   for i in 1..10
      # ...
   end
   local_variables   #=> [:fred, :i]

サンプルコード

require 'pp'

class Hoge
  def hoge(msg)
    a = 1
    b = 2
    pp binding.local_variables
  end
end

Hoge.new.hoge("@@hoge@@")

出力

[:msg, :a, :b]

:page_with_curl: Binding#receiver

pry で doc を確認

サンプルコード

require 'pp'

class Hoge
  def hoge(msg)
    a = 1
    b = 2
    pp binding.receiver
    pp binding.receiver.hige
  end

  def hige
    'hige'
  end
end

Hoge.new.hoge("@@hoge@@")

出力

#<Hoge:0x007f9029c6c558>
"hige"

:book: Enumerable

:page_with_curl: slice_after

slice_before の after 版。
* るりま | Enumerable#slice_before
* issue 9071

サンプルコード

print *[1, 1, 2, 3, 3, 4, 5].slice_before(&:even?), "\n"
print *[1, 1, 2, 3, 3, 4, 5].slice_after(&:even?), "\n"

出力

[1, 1][2, 3, 3][4, 5]
[1, 1, 2][3, 3, 4][5]

:page_with_curl: slice_when

隣りあった要素を2つの引数としてブロックを呼び出して真を返したらそこで区切る。

サンプルコード

print [1, 1, 2, 3, 3, 4, 5].slice_when { |a, b| a.odd? && b.even? }.to_a

出力

[[1, 1], [2, 3, 3], [4, 5]]

:page_with_curl: min, min_by, max and max_by

複数の値を返すことが可能になった。

サンプルコード

print [*'1'..'11'].min(3), "\n"
print [*'1'..'11'].min_by(3) { |e|e.to_i }, "\n"
print [*'1'..'11'].max(3), "\n"
print [*'1'..'11'].max_by(3) { |e|e.to_i }, "\n"

出力

["1", "10", "11"]
["1", "2", "3"]
["9", "8", "7"]
["11", "10", "9"]

:book: Float

:page_with_curl: next_float, previous_float

詳しい経緯はこちら

サンプルコード

p 1.1.next_float
p 1.1.prev_float

出力

1.1000000000000003
1.0999999999999999

:book: Kernel

:page_with_curl: itself

自身を返す。
暗黙の to_proc を利用した Enumerable のメソッド呼び出しなどを簡潔に書ける

サンプルコード

random_numbers = (1..5).each_with_object([]) do |e, memo|
  rand(1..10).times { memo << e }
end
random_numbers.shuffle!
print random_numbers, "\n"
print random_numbers.group_by(&:itself)

__END__
# 従来の書き方。文字数はこっちのほうが少ないけど、ブロックを書く必要がある
print random_numbers.group_by { |e|e }

出力

  • 1回目
[5, 5, 5, 4, 5, 1, 4, 4, 2, 2, 3, 2, 2, 5, 1, 2, 1, 4, 2]
{5=>[5, 5, 5, 5, 5], 4=>[4, 4, 4, 4], 1=>[1, 1, 1], 2=>[2, 2, 2, 2, 2, 2], 3=>[3]}
  • 2回目
[4, 1, 3, 4, 5, 4, 4, 3, 3, 2, 2, 4, 2, 4, 4, 3, 4, 3, 1, 5, 5]
{4=>[4, 4, 4, 4, 4, 4, 4, 4], 1=>[1, 1], 3=>[3, 3, 3, 3, 3], 5=>[5, 5, 5], 2=>[2, 2, 2]}

:book: String

:page_with_curl: unicode_normalize, unicode_normalize!, unicode_normalized?

サンプルコード

text = "a\u0300"
p text.unicode_normalize
p text
p text.unicode_normalized?
p text.unicode_normalize!
p text
p text.unicode_normalized?

出力

"\u00E0"
"a\u0300"
false
"\u00E0"
"\u00E0"
true

:book: Method

:page_with_curl: curry

メソッドのカリー化を行う。
Proc#curry と同等のことが メソッドでもできるようになった。
るりま | Proc#curry

サンプルコード

def trapezoid(upper_base, lower_base, hight)
  (upper_base + lower_base) * hight / 2
end

trapezoid_10_20 = method(:trapezoid).curry(3).(10).(20)
p trapezoid_10_20.(2)
p trapezoid_10_20.(4)

出力

30 # => ( 10 + 20 ) * 2 / 2 => 60 / 2
60 # => ( 10 + 20 ) * 4 / 2 => 120 / 2

:page_with_curl: super_method

サンプルコード

class Parent
  def hoge
    "super hoge"
  end
end

class Child < Parent
  def hoge
    "child hoge"
  end
end

child = Child.new
p child.hoge
p child.public_method(:hoge).super_method
p child.public_method(:hoge).super_method.call

出力

"child hoge"
#<Method: Parent#hoge>
"super hoge"

:books: 脚注

95
96
3

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
95
96