以下文件只有在编辑模式下猜可以正常显示
Some of the most important improvements in ruby 1.9
- Much better threading – performance and tools are superior to ruby 1.8. Read more about threads and fibers atPaul Barry’s blog.
- Unicode support, finally. Also a brand new encoding engine has been added. There’s a very informative blog post about this onYehuda
Katz’s blog.
- The interpreter’s performance had been vastly improved. Check out the (v1.9.0)benchmark.
- Rubygems is now integrated into ‘ruby’ itself.
Let’s look at the main incompatibilites that can break your code
- The shiny new
String class.
- Literal Hash constructor has changed.
- {“a”, “b”} no longer makes a Hash but a nice syntax error. You have to use {“a” => “b”} or {:a => “b”} or {a: “b”}
Now for the other important changes and upgrades
Changes in the Hash class
- Hash finally preserves insertion order
- Ruby 1.8
hash = {:a=> 'A', :b=>'B', :c=>'C', :d=>'D'}
hash.keys ====> [:b, :c, :d, :a]
hash.values ====> ["B", "C", "D", "A"]
|
- Ruby 1.9
hash = {:a=> 'A', :b=>'B', :c=>'C', :d=>'D'}
hash.keys ====> [:a, :b, :c, :d]
hash.values ====> ["A", "B", "C", "D"]
|
- Hash#to_s behaves much nicer
- Ruby 1.8
hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
hash.to_s ====> "b2c3d4a1"
|
- Ruby 1.9
hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
hash.to_s ====> "{:a=>1, :b=>2, :c=>3, :d=>4}"
|
- Hash#select now returns a hash, not an array
- Ruby 1.8
hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
hash.select{|k,v| k == :c } ====> [[:c, 3]]
|
- Ruby 1.9
hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
hash.select{|k,v| k == :c } ====> {:c=>3}
|
String changes
- Single character strings
- Ruby 1.8
irb(main):001:0> ?c
=> 99
irb(main):001:0> "cat"[1]
=> 97
|
- Ruby 1.9
irb(main):001:0> ?c
=> "c"
irb(main):001:0> "cat"[1]
=> "a"
|
- Encoding, encoding, encoding
- All strings have an additional chunk of info attached: Encoding
ruby-1.9.2-p136 :003 > "whatever".encoding
=> #<Encoding:UTF-8>
ruby-1.9.2-p136 :004 > "whatever".encoding.name
=> "UTF-8"
|
- String#size takes encoding into account – returns the encoded character count
puts utf8_string.size # >> 6
puts latin1_string.size # >> 6
|
- You can get the raw datasize
puts utf8_string.bytesize # >> 8
puts latin1_string.bytesize # >> 6
|
- Indexed access is by encoded data – characters, not bytes
puts utf8_string[2..4] # >> sum
puts latin1_string[2..4] # >> sum
|
- You can change encoding by force but it doesn’t convert the data
my_string = "Whatever"
puts my_string.encoding.name # >> US-ASCII
my_string.force_encoding("UTF-8")
puts my_string.encoding.name # >> UTF-8
# changing the encoding doesn't convert the data though!
latin1_string.force_encoding("UTF-8")
puts latin1_string.encoding.name # >> UTF-8
puts latin1_string.bytesize # >> 6
puts latin1_string.valid_encoding? # >> false
latin1_string =~ /AR/ # !> ArgumentError: invalid byte sequence in UTF-8
|
- You can re-encode a string to ‘fix’ the above ‘error’
transcoded_utf8_string= latin1_string.encode("UTF-8")
puts transcoded_utf8_string.valid_encoding? # >> true
|
- Iterators have changed as String is not Enumerable anymore
#Use each_byte, each_char or each_codepoint
utf8_resume.each_byte do |byte|
puts byte
end
# >> 82
# >> 195
# >> 169
# >> 115
# >> 117
# >> 109
# >> 195
# >> 169
utf8_resume.each_char do |char|
puts char
end
# >> R
# >> é
# >> s
# >> u
# >> m
# >> é
utf8_resume.each_codepoint do |codepoint|
puts codepoint
end
# >> 82
# >> 233
# >> 115
# >> 117
# >> 109
# >> 233
# if you need custom processing, you can get the enumerators with bytes, chars, lines and codepoints
p utf8_resume.bytes.first(3)
# >> [82, 195, 169]
p utf8_resume.chars.find { |char| char.bytesize > 1 }
# >> "é"
p utf8_resume.codepoints.to_a
# >> [82, 233, 115, 117, 109, 233]
p utf8_resume.lines.map { |line| line.reverse }
# >> ["émuséR"]
|
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
|
tr and Regexp are Unicode-compatible
You can now specify source file encoding
Basic
# coding: utf-8
Emacs
# -*- encoding: utf-8 -*-
Shebang
#!/usr/local/rubybook/bin/ruby
# encoding: utf-8
Inject methods
#Ruby 1.9
[1,2].inject(:+)
#Ruby 1.8.6
[1,2].inject {|a,b| a+b}
|
Lambda shorthand syntax
#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
|
Multi-splat
# This will work on both Ruby 1.8 and 1.9
a, b, c = *[1, 2], 3
# This will fail on 1.8, but work on 1.9
a, b, c = 1, *[2, 3]
# Even this will work on 1.9, but not 1.8
a, b, c, d, e, f = *[1, 2], 3, *[4, 5]
|
Blocks now can accept block arguments
define_method(:answer) { |&b| b.call(42) }
分享到:
相关推荐
var picked = cherrypick(someObject, 'a b'); 选择将是: { a:1, b:2 } 您可以通过将 true 作为第二个参数传递来切换到排除模式: var someObject = { a:1, b:2, c:3 }; var picked = cherrypick...
No bullshit. Full of problem, solutions & how it works! Enjoy.
Ajax-movie-picked.zip,创建电影精选应用程序的挑战是尝试模拟http://www.imdb.com的后端和前端功能/,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态...
- MINOR: halog: add some help on the command line (cherry picked from commit 615674cdec067066a42f53f5d55628ab7b207e6c) - BUILD: fix build error on FreeBSD - BUG: queue: fix dequeueing sequence on ...
"you picked blue" ) end 你也可以使用 Turtle,就像 Hackety-Hack 一样 Turtle . start do background yellow pencolor brown pensize 2 goto 30 , 200 setheading 180 1000 . time
blog.pickedup.org
Swift 4 Recipes,Hundreds of Useful Hand-picked Code Snippets,2019.
If you have picked up this book, chances are you know a little bit about Swift Programming. With Practical Swift you’ll develop an advanced understanding of the language that will enable you to ...
pytest-picked:运行与更改的文件相关的测试(根据Git))
Unity5.6.4打包APK报错,安装 android-sdk_r24.4.1-windows 解决,亲测可用!
We’ve picked up a deep understanding of the toolset, frameworks, and programming language—an understanding that is cru‐ cial to building the best possible software for iOS and OS X. Apple ...
At the Symposium on Principles of Distributed Computing in the year 2000, Eric Brewer held a keynote talk about his experience with the recent changes in the development of distributed databases....
randumb是Ruby,可以让您轻松地从所选数据库中提取随机记录。 需要ActiveRecord> = 3.0.0,并支持SQLite,MySQL和Postgres / PostGIS(欢迎PR提供其他数据库支持)。 安装 # Add the following to you Gemfile gem...
JAVA文件不大一般手机都支持,支持3W网页浏览
the user of a randomly picked query than for a randomly picked user. We show that users exhibit consistent topical interests that vary between users. We also see that user clicks indicate a variety of...
Releases through to 0.3.0 progressed slowly, though the new 0.4.0 release has picked up speed, become more reliable, and supports Win32 much better. Also supports Solaris, and will likely support ...
I picked OpenGL ES 2.0 and Android as an implementation environment. ...and: Ruboto integration endeavor for PV249 The idea is to provide a way of executing some simple geometric algorithms ...
Pick Up是一款使用JavaScript和Unity Script在Unity 3D中制作的桌面游戏。 我们正在寻找开发人员。...随意下载我们的项目文件,并进行一些更改。 如果您想在自己的项目中使用脚本,可以随意使用,但是请给我们一些荣誉...
Arthas(阿尔萨斯)是阿里巴巴开源的Java诊断工具,深受开发者喜爱。当你遇到以下类似问题而束手无策时,Arthas 可以帮助你解决:这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?...
电影评论 “电影评论”应用程序的创建面临着尝试模拟后端和前端功能的挑战 检查Wiki以获取更多信息: : 工作示例: :