🕒

Ruby 3.0.0-devを試す

1 min read

Setup

  • rbevn最新版で取得できます
  • anyenvを使っているので下記の手順
anyenv update
rbenv install --list-all | grep 3.0.0
# 3.0.0-dev
rbenv install 3.0.0-dev

適当なディレクトリで切り替えます

mkdir ruby3
cd ruby3
rbenv local 3.0.0-dev

Symbol:#name

irb(main):001:0> :a.name.frozen?
=> true
irb(main):002:0> :a.to_s.frozen?
=> false

右代入

irb(main):001:0> [170, 65] => height, weight
=> [170, 65]
irb(main):002:0> height
=> 170

エンドレスメソッド定義構文

irb(main):001:0> def inc(x) = x + 1
=> :inc
irb(main):002:0> p inc(42)
43
=> 43

Ractor

def fib n
  if n < 2
    1
  else
    fib(n-2) + fib(n-1)
  end
end

RN = 10
rs = (1..RN).map do |i|
  Ractor.new i do |i|
    [i, fib(i)]
  end
end

until rs.empty?
  r, v = Ractor.select(*rs)
  rs.delete r
  p answer: v
end

https://github.com/ruby/ruby/blob/master/doc/ractor.md#examples を見ると良いかもです。