[英]What is the difference between Ruby 1.8 and Ruby 1.9
我不清楚“当前”版本的Ruby(1.8)和“新”版本(1.9)之间的差异。 是否存在对这些差异的“简单”或“简单”解释以及为何如此不同?
Sam Ruby有一个很酷的幻灯片,概述了差异 。
为了便于参考这些信息,如果链接在抽象的未来失效,这里是对Sam的幻灯片的概述。 幻灯片放映的评论不那么令人难以置信,但是将它们全部列在这样的列表中也很有帮助。
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
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
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
现在已经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"}
订单是广告订单
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]}
pack
和unpack
pack
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
比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
操作:使用:: 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>
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> {}.methods.sort.last
=> :zip
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?
#!/usr/local/rubybook/bin/ruby
# encoding: 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
[1,2].each {|value; t| t=value*value}
Ruby 1.9
[1,2].inject(:+)
Ruby 1.9
[1,2].inject(:+)
Ruby 1.8.6
short_enum = [1, 2, 3].to_enum
long_enum = ('a'..'z').to_enum
loop do
puts "#{short_enum.next} #{long_enum.next}"
end
to_enum
Ruby 1.9
e = [1,2,3].each
Ruby 1.9
p = -> a,b,c {a+b+c}
puts p.(1,2,3)
puts p[1,2,3]
Ruby 1.9
p = -> a,b,c {a+b+c} puts p.(1,2,3) puts p[1,2,3]
Ruby 1.8.6
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.9
/\\p{Space}/
Ruby 1.8.6
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
Ruby 1.9
def toggle def toggle "subsequent times" end "first time" end
HTH!
更多变化:
返回splat单例数组:
def function
return *[1]
end
a=function
数组参数
def function(array)
array.each { |v| p v }
end
function "1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.