`

Ruby1.8和Ruby1.9的不同

阅读更多

Someone recently emailed the ruby-core mailing list asking "Is there some list of 'bullet points' on the major differences between the syntax of Ruby 1.8 and Ruby 1.9 available somewhere?" The response, of course was a link to the definitive list of changes in Ruby 1.9.

But that is an exhaustive list instead of just highlighting the major changes. So, the following is my somewhat more digested list of the important changes, as I understand them. Additions, corrections, clarifications, and so forth are welcome in the comments.

Text

  • Characters are represented by single-character strings, rather than integers:
    • ?A returns "A" instead of 65
    • "HELLO"[1] returns "E" instead of 69. s[x] is now the same as s[x,1]
    • Use ord method of String to get character encoding. It returns the encoding of the first character of the string
  • Strings are no longer Enumerable, and the each method has been removed. Use each_line and each_byte to iterate lines and bytes. Both of these methods can return enumerators (see below), which are Enumerable.
  • Ruby 1.9 adopts the Oniguruma regexp engine, which adds advanced new features for regular expression wizards.
  • Additional changes are expected to Ruby's Unicode and multi-byte string support, but Matz has not unveiled them yet. Strings may have an encoding method for querying or setting their encoding.

Ranges

  • member? and include? work differently if the endpoints of a range are not numbers: they actually iterate with succ to test membership in that case.
  • The new method covers? does what member? and include? did in 1.8

Hashes

  • New hash syntax. When the keys of a hash are symbols, you can move the colon from the beginning of the symbol to the end (no space allowed) and omit the =>. So this hash {:a=>1,:b=>2} turns into {a:1,b:2}. The Ruby 1.8 syntax is still supported, of course.

Parallel Assignment

  • Any number of splat operators may appear on the right-hand side of a parallel assignment in Ruby 1.9. Previously only the last rvalue could have a splat.
  • The left-hand side of a parallel assignment may have only one splat operator, as always, but it is no longer required to be on the last lvalue. In Ruby 1.9 a splat may appear before any one lvalue.
  • Remember that the rules of parallel assignment apply to block invocation as well: the arguments to yield are rvalues, and the block parameters are lvalues. So these splat changes apply to blocks as well.
  • In Ruby 1.8, the value of a parallel assignment expression was an array of the lvalues. For efficiency, Ruby 1.9 evaluates all parallel assignments to true.

Enumerators

  • The iterator methods of core classes and modules like String, Fixnum, Array, Hash and Enumerable now return an enumerator object when invoked with no block. An enumerator is an Enumerable object. The enumerator return by each of these iterator methods uses that underlying iterator in place of the each method normally used by the Enumerable mixin. So we can write things like:
    
    counter = (1..10).each  # returns an enumerator 
    counter.each_with_index { |n,i| puts n,i }
    
  • Ruby makes Enumerable::Enumerator core, so you no longer have to require "enumerator" to get methods like enum_for

Blocks

  • Block parameters are always local to their blocks, even when the enclosing scope includes a variable by the same name. Use -w to get a warning when this will change the behavior of your code.
  • Block parameters must be local variables in Ruby 1.9. No more assigning to instance variables or global variables as a side-effect of block invocation
  • You can declare block-local variables in Ruby 1.9. Just follow the ordinary list of block parameters with a semi-colon and follow it with a comma-separated list of variable names:
    
    hash.each { |k,v; x,y,z| ... }
    
    With this block declaration, x, y, and z will be local to the block, even if they are already defined in the enclosing scope.
  • As per the parallel-assignment changes described above a block parameter list may a splat operator before any one parameter. It is no longer required to be the last one.
  • The last block parameter may be prefixed with an ampersand to make it receive a block, just as you can do with methods. This is typically only useful when the block is being turned into a proc or lambda

Procs and Lambdas

  • Kernel.proc is now a synonym for Proc.new: proc now creates a proc and lambda creates a lambda. (Both procs and lambdas are still instances of Proc, of course.)
  • The Symbol.to_proc method is now built-in to Ruby.
  • Ruby 1.9 supports a (strange at first) new syntax for defining lambdas:
    
    ->(x,y) { x + y }  # same as lambda {|x,y| x + y}
    
    • Parentheses are optional in this new syntax:
      
      ->x,y { x + y }  # same as lambda {|x,y| x + y}
      
    • The new lambda syntax supports block-local variable declarations following a semicolon just as the regular block syntax does.
    • The new lambda syntax allows argument defaults using the same syntax as method declarations:
      
      sale_price = ->(price,discount=.25) { (1.0-discount)*price }
      
  • Procs and lambdas can be invoked with parentheses preceded by a period. Given a lambda sum, the following three lines are synonyms:
    
    sum.call(1,2)
    sum[1,2]
    sum.(1,2)
    
  • Procs now have a yield method that is an alternative to call. yield uses yield semantics rather than method calling semantics to invoke the proc. This means that it is more relaxed about arity mis-matches and behaves like parallel assignment when the argument is a single array.

Bindings

  • Binding objects have an eval method to evaluate in that binding. This is an alternative to passing the binding as the second argument to Kernel.eval.
  • Proc.binding is now a private method. It is not clear if this is a bug or if that method will no longer be available.

Continuations

  • Continuations are not supported in Ruby 1.9

Private Methods

  • The method name resolution algorithm has changed or may be changing to alter the way private methods are looked up. The details are still unclear (to me, at least).

Class Variables

  • Class variables are no longer shared by a class and its subclasses. A subclass can read the values of class variables defined by its superclass. But if it sets the value of such a variable, it simply creates its own local copy of the variable, and no longer alters the value seen by the superclass.

Math

  • Math.log2 computes base-2 log
  • Math.log(x,y) computes the log base-y of x

———————————————————————野蛮小分割————————————————————————————

 

另外一个介绍

分享到:
评论
1 楼 yuan 2011-01-02  
试了一下ruby1.9.2,好像Class Variables的问题并没有修正呀。

相关推荐

    Ruby编程语言_涵盖Ruby 1.8和1.9

    Ruby编程语言_涵盖Ruby 1.8和1.9

    Ruby程序设计语言 (涵盖Ruby 1.8和1.9)源代码

    《Ruby程序设计语言》是Ruby的权威指南,全面涵盖该语言的1.8版和1.9版。本书详尽但并不拘泥于语言规范,既适合首次接触Ruby的资深程序员,同样也适合那些想要挑战对这门语言的理解并更深入掌握它的Ruby程序员。本书...

    Ruby编程语言_涵盖Ruby 1.8和1.9 中文

    要学 Ruby 的话,赶快下载,很好的马来亚书

    七牛RubySDK.zip

    此 Ruby SDK 适用于 Ruby 1.8.x, 1.9.x, jruby, rbx, ree 版本,基于 七牛云存储官方API 构建。使用此 SDK 构建您的网络应用程序,能让您以非常便捷地方式将数据安全地存储到七牛云存储上。无论您的网络应用是一个...

    Ruby编程语言中文版

    Ruby编程语言中文版,涵盖Ruby 1.8和Ruby 1.9

    为何Ruby 1.9的不兼容性会导致原有Ruby代码无法工作

    一些Ruby程序员将Ruby 1.9的发布看做是迈入Ruby新版本的标志性事件,但他们却发现在1.8版本和1.9版本语言之间的刻意...真正的问题是,Ruby 1.9.0发布的原因以及其与Ruby1.8之间的关系并不完全被Ruby社区元老们所了解。

    Ruby编程语言pdf

    本书详细介绍了Ruby 1.8和1.9版本各方面的内容。在对Ruby进行了简要的综述之后,本书详细介绍了以下内容:Ruby的句法和语法结构,数据结构和对象,表达式和操作符,语句和控制结构,方法、proc、lambda和闭包,反射...

    Programming Ruby 1.9 & 2.0 The Pragmatic Programmers’ Guide 4th Edition

    Ruby 2.0 is a minor update to Ruby 1.9, unlike the more major updates from Ruby 1.8 to Ruby 1.9. The major language changes in Ruby 2.0 are the addition of keyword arguments and the change to use UTF-...

    ruby教程.rar

    ruby 1.8 特性 1.6.8到1.8.0的变更点(总结) ruby 1.9 特性 obsolete 对应DOSISH 附录 疑似BNF的Ruby语法 Ruby术语集 Ruby的运行平台 pack模板字符串 sprintf格式 Marshal格式 Ruby FAQ Ruby的陷阱 ...

    垃圾:告知StatsD请求时间,GC,对象等信息。 最新的Rails 4和Ruby 2.1支持,以及古老的Rails 2和Ruby 1.8支持

    支持旧版本:Rails 2/3/4,Ruby 1.9 +,REE,带有RubyBench补丁的Ruby 1.8。 建立 滑轨5 在Rails 5(以及Rails 3和4)上,将其添加到config/application.rb的顶部: require 'trashed/railtie' 并在您的应用程序...

    midilib:纯Ruby MIDI文件和事件处理库

    midilib与Ruby 1.8.x 1.9.x和2.x兼容。 依赖关系 midilib不需要任何其他软件包。 tests目录中的测试套件需要测试框架TestUnit,该框架随Ruby 1.8及更高版本一起提供,也可以在Ruby Application Archive( )中找到...

    Ruby.Performance.Optimization

    80% of the material will also be useful for legacy Ruby 1.8 users, and there is 1.8-specific advice as well. Table of Contents Chapter 1. What Makes Ruby Code Fast Chapter 2. Fix Common Performance ...

    solutious.github.com:solutious.com的源代码-git source code

    注意:请使用Ruby 1.8.x(1.9中的语法突出显示有些问题) 第一次运行 安装宝石 sudo gem install jekyll -V --no-ri --no-rdoc sudo gem install rdiscount -V --no-ri --no-rdoc 可选依赖项: (不与Ruby 1.9...

    Ruby Programming Language 2008 pdf

    The Ruby Programming Language is the authoritative guide to Ruby and provides comprehensive coverage of versions 1.8 and 1.9 of the language. It was written (and illustrated!) by an all-star team: ...

    ruby-1.9.3-rpm:Ruby 1.9.3的RPM规范

    此规范是为了在基于RHEL的系统上推动以1.9.3+稳定地替换Ruby1.8.x。 我基于Ruby 1.9.3和Ruby Enterprise Edition的规范进行工作。如何安装RHEL / CentOS 5/6 yum install -y rpm-build rpmdevtools readline-devel ...

    Reudy19:在Ruby 1.9上运行的Chatbot Lloydy

    在Ruby 1.8上不起作用。 如何使用 当用作IRC机器人时 在公用文件夹中编辑setting.yml之后 Rubyirc_reudy.rb 将启动IRC客户端。 用作Twitter机器人时 需要Ruby和高线才能工作 宝石安装Ruby高线 请安装。 从创建一个...

    cover_me:Ruby 1.9的RCov式覆盖工具

    它比Ruby 1.8。×更快,更强大,更干净,并且具有巨大的改进。 由于这些原因,每个Ruby开发人员都应该转向我们的语言的这一激动人心的新版本。 当采取这种规模的行动时,拥有正确的工具来帮助我们至关重要。 不幸的...

Global site tag (gtag.js) - Google Analytics