Cuál es la diferencia entre Ruby 1.8 y Ruby 1.9
No tengo claras las diferencias entre la versión "actual" de Ruby (1.8) y la versión "nueva" (1.9). ¿Hay una explicación" fácil "o" simple " de las diferencias y por qué es tan diferente?
4 answers
Sam Ruby tiene una presentación de diapositivas que describe las diferencias .
En el interés de llevar esta información en línea para facilitar la referencia, y en caso de que el enlace se muera en el futuro abstracto, aquí hay una descripción general de las diapositivas de Sam. La presentación de diapositivas es menos abrumadora para revisar, pero tenerlo todo expuesto en una lista como esta también es útil.
Ruby 1.9-Mayor Características
- Rendimiento
- Hilos / fibras
- Encoding/Unicode
- gems es (en su mayoría) incorporado ahora
- las sentencias if no introducen scope en Ruby.
¿Qué ha cambiado?
Cadenas de caracteres individuales.
Ruby 1.9
irb(main):001:0> ?c
=> "c"
Ruby 1.8.6
irb(main):001:0> ?c
=> 99
Índice de cadenas.
Ruby 1.9
irb(main):001:0> "cat"[1]
=> "a"
Ruby 1.8.6
irb(main):001:0> "cat"[1]
=> 97
{"a"," b"} Ya no es compatible
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}
Acción: Convertir a {1 = > 2}
Array.to_s
Ahora Contiene Puntuación
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"
Acción: Uso .unirse en lugar
Los Dos Puntos Ya No Son Válidos En Las Declaraciones 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
Acción: Utilice punto y coma, entonces, o nueva línea
Bloquear Variables Ahora Sombra Variables locales
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
Obsoleto
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
Acción: Use Hash.key
Fixnum.to_sym
Ahora se ha ido
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
(Continuación) 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
Las Claves Hash Ahora Desordenadas
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"}
Orden es orden de inserción
Expresiones Regulares Unicode más estrictas
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
y Regexp
Ahora entiende 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
y 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
Más Brutal Que {[59]]}
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
Acción: Use:: Math:: PI
Cambios en la delegación
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>
El uso de $KCODE Produce Advertencias
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
Ahora una matriz de Símbolos
Ruby 1.9
irb(main):001:0> {}.methods.sort.last
=> :zip
Ruby 1.8.6
irb(main):001:0> {}.methods.sort.last
=> "zip"
Medidas: Reemplazar instance_methods.incluir? con method_defined?
Codificación del archivo fuente
Básico
# coding: utf-8
Emacs
# -*- encoding: utf-8 -*-
Shebang
#!/usr/local/rubybook/bin/ruby
# encoding: utf-8
Real Threading
- Condiciones de carrera
- Supuestos de Ordenación implícitos
- Código de prueba
¿Qué hay de nuevo?
Sintaxis alternativa para el Símbolo como Claves Hash
Ruby 1.9
{a: b}
redirect_to action: show
Ruby 1.8.6
{:a => b}
redirect_to :action => show
Bloquear Variables locales
Ruby 1.9
[1,2].each {|value; t| t=value*value}
Métodos de inyección
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 hay bloqueo? ¡Enum!
Ruby 1.9
e = [1,2,3].each
Lambda Taquigrafía
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)
Números complejos
Ruby 1.9
Complex(3,4) == 3 + 4.im
Decimal Todavía No Es El Valor Predeterminado
Ruby 1.9
irb(main):001:0> 1.2-1.1
=> 0.0999999999999999
Regex"Propiedades"
Ruby 1.9
/\p{Space}/
Ruby 1.8.6
/[:space:]/
Splat en el medio
Ruby 1.9
def foo(first, *middle, last)
(->a, *b, c {p a-c}).(*5.downto(1))
Fibras
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}
Valores de ruptura
Ruby 1.9
match =
while line = gets
next if line =~ /^#/
break line if line.find('ruby')
end
Métodos"anidados"
Ruby 1.9
def toggle
def toggle
"subsequent times"
end
"first time"
end
HTH!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-21 20:52:27
Una gran diferencia sería el paso del intérprete de Matz a YARV, una máquina virtual de código de bytes que ayuda significativamente con el rendimiento.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-22 03:11:30
Muchos ahora recomiendan El Lenguaje de programación Ruby sobre el Pico - más al punto, tiene todos los detalles de las diferencias 1.8/1.9.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-15 09:31:30
Algunos cambios más:
Devolviendo una matriz splat singleton:
def function
return *[1]
end
a=function
- ruby 1.9: [1]
- ruby 1.8: 1
Argumentos de matriz
def function(array)
array.each { |v| p v }
end
function "1"
- ruby 1.8: "1"
- ruby 1.9:método indefinido `each' para "1": Cadena
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-08 15:13:32