Ruby 1.8 和 Ruby 1.9 有什么区别

发布于 2024-07-04 10:11:23 字数 82 浏览 9 评论 0原文

我不清楚 Ruby 的“当前”版本(1.8)和“新”版本(1.9)之间的区别。 对于这些差异以及为什么如此不同,是否有一个“简单”或“简单”的解释?

I'm not clear on the differences between the "current" version of Ruby (1.8) and the "new" version (1.9). Is there an "easy" or a "simple" explanation of the differences and why it is so different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫雨熙 2024-07-11 10:11:23

一个巨大的区别是从 Matz 的解释器迁移到 YARV,这是一种字节码虚拟机,可以显着帮助表现。

One huge difference would be the move from Matz's interpreter to YARV, a bytecode virtual machine that helps significantly with performance.

⊕婉儿 2024-07-11 10:11:23

Sam Ruby 有一个很酷的幻灯片,概述了差异

为了将这些信息内联起来以便于参考,并且以防万一链接在抽象的未来失效,这里是 Sam 幻灯片的概述。 幻灯片放映不太容易查看,但将所有内容都排列在这样的列表中也很有帮助。

Ruby 1.9 - 主要功能

  • 性能
  • 线程/Fibers
  • 编码/Unicode
  • gems 现在(大部分)是内置的
  • if 语句不会在 Ruby 中引入作用域。

发生了什么变化?

单字符串。

Ruby 1.9

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

Ruby 1.8.6

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

字符串索引。

Ruby 1.9

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

Ruby 1.8.6

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

{"a","b"} 不再支持

Ruby 1.9

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

Ruby 1.8.6

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

操作: 转换为 {1 => 2}


Array.to_s 现在包含标点符号

Ruby 1.9

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

Ruby 1.8.6

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

操作: 使用 .join 代替


冒号 在 When 语句中不再有效

Ruby 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'

Ruby 1.8.6

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

操作: 使用分号、then 或换行符


块变量现在隐藏局部变量

Ruby 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

Ruby 1.8.6

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

Hash.index 已弃用

Ruby 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

Ruby 1.8.6

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

操作: 使用 Hash.key


Fixnum.to_sym 现已消失

Ruby 1.9

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

Ruby 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


哈希键现在无序

Ruby 1.9

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

Ruby 1.8.6

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

顺序是插入顺序


更严格的 Unicode 正则表达式

Ruby 1.9

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

Ruby 1.8.6

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

trRegexp 现在了解 Unicode

Ruby 1.9

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

pack< /code> 和 unpack

Ruby 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

BasicObjectBlankSlate

Ruby 1.9

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

更加残酷Ruby 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


委托更改

Ruby 1.9

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

Ruby 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 会产生警告

Ruby 1.9

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

Ruby 1.8.6

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

instance_methods 现在是符号数组

Ruby 1.9

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

Ruby 1.8.6

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

操作: 替换 instance_methods.include? 与method_define?


源文件编码

基本

# coding: utf-8

Emacs

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

Shebang

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

真实线程

  • 竞争条件
  • 隐式排序假设
  • 测试代码

有什么新内容?

作为哈希键的符号的替代语法

Ruby 1.9

{a: b}

redirect_to action: show

Ruby 1.8.6

{:a => b}

redirect_to :action => show

块局部变量

Ruby 1.9

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

注入方法

Ruby 1.9

[1,2].inject(:+)

Ruby 1.8.6

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

to_enum

Ruby 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

没有块? 枚举!

Ruby 1.9

e = [1,2,3].each

Lambda 速记

Ruby 1.9

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

Ruby 1.8.6

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

复数

Ruby 1.9

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

小数仍然不是默认值

Ruby 1.9

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

正则表达式“属性”

Ruby 1.9

/\p{Space}/

Ruby 1.8.6

/[:space:]/

中间的 Splat

Ruby 1.9

def foo(first, *middle, last)

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

纤维

Ruby 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}

中断值

Ruby 1.9

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

“嵌套”方法

Ruby 1.9

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

哈!

Sam Ruby has a cool slideshow that outline the differences.

In the interest of bringing this information inline for easier reference, and in case the link goes dead in the abstract future, here's an overview of Sam's slides. The slideshow is less overwhelming to review, but having it all laid out in a list like this is also helpful.

Ruby 1.9 - Major Features

  • Performance
  • Threads/Fibers
  • Encoding/Unicode
  • gems is (mostly) built-in now
  • if statements do not introduce scope in Ruby.

What's changed?

Single character strings.

Ruby 1.9

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

Ruby 1.8.6

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

String index.

Ruby 1.9

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

Ruby 1.8.6

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

{"a","b"} No Longer Supported

Ruby 1.9

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

Ruby 1.8.6

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

Action: Convert to {1 => 2}


Array.to_s Now Contains Punctuation

Ruby 1.9

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

Ruby 1.8.6

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

Action: Use .join instead


Colon No Longer Valid In When Statements

Ruby 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'

Ruby 1.8.6

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

Action: Use semicolon, then, or newline


Block Variables Now Shadow Local Variables

Ruby 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

Ruby 1.8.6

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

Hash.index Deprecated

Ruby 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

Ruby 1.8.6

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

Action: Use Hash.key


Fixnum.to_sym Now Gone

Ruby 1.9

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

Ruby 1.8.6

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

(Cont'd) 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


Hash Keys Now Unordered

Ruby 1.9

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

Ruby 1.8.6

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

Order is insertion order


Stricter Unicode Regular Expressions

Ruby 1.9

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

Ruby 1.8.6

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

tr and Regexp Now Understand Unicode

Ruby 1.9

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

pack and unpack

Ruby 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 More Brutal Than BlankSlate

Ruby 1.9

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

Ruby 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

Action: Use ::Math::PI


Delegation Changes

Ruby 1.9

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

Ruby 1.8.6

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

Defect 17700


Use of $KCODE Produces Warnings

Ruby 1.9

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

Ruby 1.8.6

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

instance_methods Now an Array of Symbols

Ruby 1.9

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

Ruby 1.8.6

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

Action: Replace instance_methods.include? with method_defined?


Source File Encoding

Basic

# coding: utf-8

Emacs

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

Shebang

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

Real Threading

  • Race Conditions
  • Implicit Ordering Assumptions
  • Test Code

What's New?

Alternate Syntax for Symbol as Hash Keys

Ruby 1.9

{a: b}

redirect_to action: show

Ruby 1.8.6

{:a => b}

redirect_to :action => show

Block Local Variables

Ruby 1.9

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

Inject Methods

Ruby 1.9

[1,2].inject(:+)

Ruby 1.8.6

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

to_enum

Ruby 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

No block? Enum!

Ruby 1.9

e = [1,2,3].each

Lambda Shorthand

Ruby 1.9

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

Ruby 1.8.6

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

Complex Numbers

Ruby 1.9

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

Decimal Is Still Not The Default

Ruby 1.9

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

Regex “Properties”

Ruby 1.9

/\p{Space}/

Ruby 1.8.6

/[:space:]/

Splat in Middle

Ruby 1.9

def foo(first, *middle, last)

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

Fibers

Ruby 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}

Break Values

Ruby 1.9

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

“Nested” Methods

Ruby 1.9

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

HTH!

╰沐子 2024-07-11 10:11:23

更多更改:

返回 splat 单例数组:

def function
  return *[1]
end

a=function
  • ruby​​ 1.9 : [1]
  • ruby​​ 1.8 : 1

数组参数

def function(array)
  array.each { |v| p v }
end
function "1"
  • ruby​​ 1.8: "1"
  • ruby​​ 1.9: undefined method `each'对于“1”:字符串

Some more changes:

Returning a splat singleton array:

def function
  return *[1]
end

a=function
  • ruby 1.9 : [1]
  • ruby 1.8 : 1

array arguments

def function(array)
  array.each { |v| p v }
end
function "1"
  • ruby 1.8: "1"
  • ruby 1.9: undefined method `each' for "1":String
錯遇了你 2024-07-11 10:11:23

现在,许多人推荐 Ruby 编程语言而不是 Pickaxe - 更重要的是,它具有 1.8/1.9 差异的所有细节。

Many now recommend The Ruby Programming Language over the Pickaxe - more to the point, it has all the details of the 1.8/1.9 differences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文