8. Ruby 1.9
• Ruby 1.9.0 release development version in
2007/12/25
• Ruby 1.9.1 release stable/production version
in 2009/1/30
• Better performance
• Better encoding support
19. case syntax
name = case name = case
when x == 1 : quot;onequot; when x == 1
1.8 when x == 2 : quot;twoquot; quot;onequot;
1.8,1.9
else quot;manyquot; when x == 2
end quot;twoquot;
else
quot;manyquot;
end
name = case
when x == 1 then quot;onequot;
when x == 2 then quot;twoquot;
1.8,1.9
else quot;manyquot;
end
20. block’s parameter always local
local
x = quot;fooquot;
y = quot;barquot;
[1,2,3].each do |x|
y=x+1
end
1.8 [x,y] => [3, 4] 1.9 [x,y] => [quot;fooquot;, 4]
21. block’s parameter always local
local
x = quot;fooquot;
y = quot;barquot;
[1,2,3].each do |x|
y=x+1
end
1.8 [x,y] => [3, 4] 1.9 [x,y] => [quot;fooquot;, 4]
22. block-local variable
local
bar = quot;ihowerquot;
1.9
bar = quot;ihowerquot;
1.8,1.9
[1,2,3].each do |val; bar|
[1,2,3].each do |val|
bar = val
foo = val
end
bar = val
end
bar => quot;ihowerquot;
foo # NameError: undefined
local variable or method `foo’
bar => 3
23. block-local variable
local
bar = quot;ihowerquot;
1.9
bar = quot;ihowerquot;
1.8,1.9
[1,2,3].each do |val; bar|
[1,2,3].each do |val|
bar = val
foo = val
end
bar = val
end
bar => quot;ihowerquot;
foo # NameError: undefined
local variable or method `foo’
bar
bar => 3
24. block can accept block argrments
proc1 = lambda do |a, *b, &block|
pa
pb
pc
block.call
end
proc1.call(1,2,3,4) { puts quot;in block1quot; }
#
1
[2,3,4]
quot;in block1quot;
26. def my_if(condition, then_clause, else_clause)
if condition
then_clause.call
else
else_clause.call
end
end
5.times do |val|
my_if (val < 3),
-> { puts quot;#{val} is smallquot; },
-> { puts quot;#{val} is bigquot; }
end
#
0 is small
1 is small
2 is small
3 is big
4 is big
27. def my_while(cond, &body)
while cond.call
body.call
end
end
a=0
my_while -> { a < 3 } do
puts a
a += 1
end
#
0
1
2
28. Kernel.proc
, do not use it!
• proc lambda
1.8
•
1.9 proc Proc.new
39. Enumerator objects are
also enumerable
1.9
seq.first(10)
# => [1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
seq.to_a
# Don’t do this if your Enumerator has infinite elements
43. Ruby 1.8 Regexp
UTF-8
• Support None (n or N), EUC (e or E),
Shift_JIS (s or S), UTF-8 (u or U)
>> quot; quot;.scan(/./u)
1.8,1.9
=> [quot; quot;, quot; quot;]
44. Use Regexp to handle String
Regexp
>> quot; quot;.scan(/./u).reverse.join
1.8,1.9
=> quot; quot;
>> quot; quot;.scan(/./u).size
=> 2
53. All String are Encoded
• In Ruby 1.9 a String is a collection of
encoded characters.
raw bytes Encoding
>> quot; quot;.encoding.name
1.9
=> quot;UTF-8quot;
Encoding
object
58. Transcoding fails
str = quot;Résuméquot;
1.9 str.encode(quot;big5quot;)
=> Encoding::UndefinedConversionError: quot;xC3xA9quot;
from UTF-8 to Big5
from (irb):2:in `encode'
from (irb):2
from /usr/local/bin/irb19:12:in `<main>'
59. Force Transcoding
byte data
utf8 = quot; quot;
1.9
big5 = utf8.encode(quot;big5quot;)
big5.valid_encoding?
=> true
big5.force_encoding(quot;utf-8quot;)
big5.valid_encoding?
=> false
60. Force Transcoding fails
big5.valid_encoding? # false
1.9 big5 =~ /123456/
=> ArgumentError: invalid byte sequence in UTF-8
from (irb):11
from /usr/local/bin/irb19:12:in `<main>'
70. IO Object
1.9
# encoding: utf-8
f = File.open(quot;another_big5.txtquot;, quot;w:big5quot;)
puts f.external_encoding # Big5
data = quot; quot;
puts data.encoding.name # UTF-8
f << data
71. IO Object
1.9
# encoding: utf-8
f = File.open(quot;another_big5.txtquot;, quot;w:big5quot;)
puts f.external_encoding # Big5
data = quot; quot;
puts data.encoding.name # UTF-8
f << data
72. Regexp has encoding too
• Oniguruma engine
1.9
http://www.geocities.jp/kosako3/oniguruma/
/w/.encoding
• Same basic API
=> #<Encoding:US-ASCII>
• better performance
/ /.encoding
=> #<Encoding:UTF-8>
• support for a lot of encoding
• extended syntax
73. Default Encoding Overview
• String literal program source
• Symbols Regexp 7-bit
US-ASCII program source
• IO external_encoding OS
• ruby -E
• Encoding.external_encoding
76. f = Fiber.new {
1.9
2.times do
puts quot;Fiber say hiquot;
Fiber.yield #
puts quot;Fiber say byequot;
end
}#
>> f.resume
# Fiber say hi
>> f.resume
# Fiber say bye
# Fiber say hi
>> f.resume
# Fiber say bye
>> f.resume
FiberError: dead fiber called
from (irb):78:in `resume'
from (irb):78
from /usr/local/bin/irb19:12:in `<main>'