Ruby 1.8和Ruby 1.9有什么区别

时间:2020-03-05 18:41:51  来源:igfitidea点击:

我不清楚Ruby的"当前"版本(1.8)和"新"版本(1.9)之间的区别。对于差异有"简单"或者"简单"的解释吗,为什么如此不同?

解决方案

回答

Sam Ruby的幻灯片很酷,概述了这些差异。

为了使此信息内联以便于参考,以防万一,如果链接在抽象的未来中消失了,这里将对Sam的幻灯片进行概述。幻灯片放映的回顾性不强,但将其全部排列在这样的列表中也很有帮助。

  • 表现
  • 线/纤维
  • 编码/ 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

行动:然后,使用分号或者换行符

块变量现在隐藏局部变量

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

tr和Regexp现在可以理解Unicode

Ruby 1.9

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

packunpack

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_defined?

源文件编码

基本的

# coding: utf-8

埃马克斯

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

舍邦

#!/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:]/

中部摔得痛

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

HTH!

回答

一个巨大的差异是从Matz的解释器到YARV的转变,YARV是一个字节码虚拟机,可显着提高性能。

回答

现在,许多人甚至比起镐来更推荐使用Ruby编程语言,它具有1.8 / 1.9差异的所有详细信息。