質問

Ruby の「現在の」バージョン (1.8) と「新しい」バージョン (1.9) の違いがよくわかりません。違いとなぜこれほど異なるのかについて「簡単に」または「簡単に」説明できるものはありますか?

役に立ちましたか?

解決

サム・ルビーは 違いを説明するクールなスライドショー.

この情報を簡単に参照できるようにインラインに表示し、抽象的な将来にリンクが切れた場合に備えて、サムのスライドの概要をここに示します。スライドショーを確認するのはそれほど難しくありませんが、このようにすべてをリストにまとめておくと便利です。

Ruby 1.9 - 主な機能

  • パフォーマンス
  • 糸/繊維
  • エンコード/Unicode
  • gems は (ほとんど) 組み込まれています
  • if ステートメントは Ruby ではスコープを導入しません。

何が変わったのでしょうか?

単一の文字列。

ルビー 1.9

irb(main):001:0> ?c
=> "c"

ルビー 1.8.6

irb(main):001:0> ?c
=> 99

文字列インデックス。

ルビー 1.9

irb(main):001:0> "cat"[1]
=> "a"

ルビー 1.8.6

irb(main):001:0> "cat"[1]
=> 97

{"a","b"} はサポートされなくなりました

ルビー 1.9

irb(main):002:0> {1,2}
SyntaxError: (irb):2: syntax error, unexpected ',', expecting tASSOC

ルビー 1.8.6

irb(main):001:0> {1,2}
=> {1=>2}

アクション: {1 => 2} に変換します


Array.to_s 句読点が含まれるようになりました

ルビー 1.9

irb(main):001:0> [1,2,3].to_s
=> "[1, 2, 3]"

ルビー 1.8.6

irb(main):001:0> [1,2,3].to_s
=> "123"

アクション: 代わりに .join を使用してください


When ステートメントではコロンが無効になりました

ルビー 1.9

irb(main):001:0> case 'a'; when /\w/: puts 'word'; end
SyntaxError: (irb):1: syntax error, unexpected ':',
expecting keyword_then or ',' or ';' or '\n'

ルビー 1.8.6

irb(main):001:0> case 'a'; when /\w/: puts 'word'; end
word

アクション: セミコロン、then、または改行を使用してください


ブロック変数がローカル変数をシャドウするようになりました

ルビー 1.9

irb(main):001:0> i=0; [1,2,3].each {|i|}; i
=> 0
irb(main):002:0> i=0; for i in [1,2,3]; end; i
=> 3

ルビー 1.8.6

irb(main):001:0> i=0; [1,2,3].each {|i|}; i
=> 3

Hash.index 廃止されました

ルビー 1.9

irb(main):001:0> {1=>2}.index(2)
(irb):18: warning: Hash#index is deprecated; use Hash#key
=> 1
irb(main):002:0> {1=>2}.key(2)
=> 1

ルビー 1.8.6

irb(main):001:0> {1=>2}.index(2)
=> 1

アクション: Hash.key を使用する


Fixnum.to_sym ナウ・ゴーン

ルビー 1.9

irb(main):001:0> 5.to_sym
NoMethodError: undefined method 'to_sym' for 5:Fixnum

ルビー 1.8.6

irb(main):001:0> 5.to_sym
=> nil

(続き) Ruby 1.9

# Find an argument value by name or index.
def [](index)
  lookup(index.to_sym)
end

svn.ruby-lang.org/repos/ruby/trunk/lib/rake.rb


ハッシュキーの順序がなくなりました

ルビー 1.9

irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"}
=> {:a=>"a", :c=>"c", :b=>"b"}

ルビー 1.8.6

irb(main):001:0> {:a=>"a", :c=>"c", :b=>"b"}
=> {:a=>"a", :b=>"b", :c=>"c"}

オーダーは広告掲載オーダーです


より厳密な Unicode 正規表現

ルビー 1.9

irb(main):001:0> /\x80/u
SyntaxError: (irb):2: invalid multibyte escape: /\x80/

ルビー 1.8.6

irb(main):001:0> /\x80/u
=> /\x80/u

tr そして Regexp Unicode を理解しましょう

ルビー 1.9

unicode(string).tr(CP1252_DIFFERENCES, UNICODE_EQUIVALENT).
  gsub(INVALID_XML_CHAR, REPLACEMENT_CHAR).
  gsub(XML_PREDEFINED) {|c| PREDEFINED[c.ord]}

pack そして unpack

ルビー 1.8.6

def xchr(escape=true)
  n = XChar::CP1252[self] || self
  case n when *XChar::VALID
    XChar::PREDEFINED[n] or 
      (n>128 ? n.chr : (escape ? "&##{n};" : [n].pack('U*')))
  else
    Builder::XChar::REPLACEMENT_CHAR
  end
end
unpack('U*').map {|n| n.xchr(escape)}.join

BasicObject より残忍な BlankSlate

ルビー 1.9

irb(main):001:0> class C < BasicObject; def f; Math::PI; end; end; C.new.f
NameError: uninitialized constant C::Math

ルビー 1.8.6

irb(main):001:0> require 'blankslate'
=> true
irb(main):002:0> class C < BlankSlate; def f; Math::PI; end; end; C.new.f
=> 3.14159265358979

アクション: ::Math::PI を使用する


委任の変更

ルビー 1.9

irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> String

ルビー 1.8.6

irb(main):002:0> class C < SimpleDelegator; end
=> nil
irb(main):003:0> C.new('').class
=> C
irb(main):004:0>

欠陥 17700


$KCODE を使用すると警告が生成される

ルビー 1.9

irb(main):004:1> $KCODE = 'UTF8'
(irb):4: warning: variable $KCODE is no longer effective; ignored
=> "UTF8"

ルビー 1.8.6

irb(main):001:0> $KCODE = 'UTF8'
=> "UTF8"

instance_methods シンボルの配列になりました

ルビー 1.9

irb(main):001:0> {}.methods.sort.last
=> :zip

ルビー 1.8.6

irb(main):001:0> {}.methods.sort.last
=> "zip"

アクション: instance_methods.include を置き換えますか?メソッド定義で?


ソースファイルのエンコーディング

基本

# coding: utf-8

Emacs

# -*- encoding: utf-8 -*-

シバン

#!/usr/local/rubybook/bin/ruby
# encoding: utf-8

リアルスレッディング

  • 競合状態
  • 暗黙的な順序付けの仮定
  • テストコード

新着情報?

ハッシュキーとしてのシンボルの代替構文

ルビー 1.9

{a: b}

redirect_to action: show

ルビー 1.8.6

{:a => b}

redirect_to :action => show

ブロックローカル変数

ルビー 1.9

[1,2].each {|value; t| t=value*value}

メソッドの注入

ルビー 1.9

[1,2].inject(:+)

ルビー 1.8.6

[1,2].inject {|a,b| a+b}

to_enum

ルビー 1.9

short_enum = [1, 2, 3].to_enum
long_enum = ('a'..'z').to_enum
loop do
  puts "#{short_enum.next} #{long_enum.next}"
end

ブロックはありませんか?えんむ!

ルビー 1.9

e = [1,2,3].each

ラムダ速記法

ルビー 1.9

p = -> a,b,c {a+b+c}
puts p.(1,2,3)
puts p[1,2,3]

ルビー 1.8.6

p = lambda {|a,b,c| a+b+c}
puts p.call(1,2,3)

複素数

ルビー 1.9

Complex(3,4) == 3 + 4.im

10 進数はまだデフォルトではありません

ルビー 1.9

irb(main):001:0> 1.2-1.1
=> 0.0999999999999999

正規表現の「プロパティ」

ルビー 1.9

/\p{Space}/

ルビー 1.8.6

/[:space:]/

真ん中のスプラット

ルビー 1.9

def foo(first, *middle, last)

(->a, *b, c {p a-c}).(*5.downto(1))

繊維

ルビー 1.9

f = Fiber.new do
  a,b = 0,1
  Fiber.yield a
  Fiber.yield b
  loop do
    a,b = b,a+b
    Fiber.yield b
  end
end
10.times {puts f.resume}

ブレーク値

ルビー 1.9

match =
   while line = gets
     next if line =~ /^#/
     break line if line.find('ruby')
   end

「ネストされた」メソッド

ルビー 1.9

def toggle
  def toggle
    "subsequent times"
  end
  "first time"
end

ひーっ!

他のヒント

大きな違いの 1 つは、Matz の通訳から ヤーフ, 、パフォーマンスを大幅に向上させるバイトコード仮想マシン。

今では多くの人が推奨しています Ruby プログラミング言語 ツルハシの上 - さらに言えば、1.8/1.9 の違いの詳細がすべて記載されています。

さらにいくつかの変更:

スプラット シングルトン配列を返す:

def function
  return *[1]
end

a=function
  • ルビー1.9:[1]
  • ルビー1.8:1

配列引数

def function(array)
  array.each { |v| p v }
end
function "1"
  • ルビー1.8:「1」
  • ルビー1.9:「1」の未定義メソッド「each」:String
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top