More Related Content Similar to Ruby 1.9 Introduction (20) Ruby 1.9 Introduction5. Ruby1.9.1
What is it?
“ Ruby 1.9 is a new series of Ruby. It is modern,
faster, with clearer syntax, multilingualized,
a much improved version of Ruby.
8. Ruby1.9.1
Faster Maybe another
presentation?
New virtual machine YARV
Kernal (native) threads
10. Ruby1.9.1
Multilingualized
File Level Encoding
11. Ruby1.9.1
Multilingualized
File Level Encoding
# encodeing: utf-8
alias π = Math::PI
12. Ruby1.9.1
Multilingualized
String Level Encoding
13. Ruby1.9.1
Multilingualized
String Level Encoding
my_string.encode(“iso-8859-1”)
14. Ruby1.9.1
Multilingualized
IO Level Encoding
15. Ruby1.9.1
Multilingualized
IO Level Encoding
f.open(‘file.txt’, ‘r:ascii’)
data = f.read
data.encoding.name
# => ‘US-ASCII’
18. Cleaner Syntax
Ruby1.9.1
String
No longer Enumerable
1.8 String.ancestors
=> [String, Enumerable, Comparable, Object, Kernel]
1.9 String.ancestors
=> [String, Comparable, Object, Kernel]
19. Cleaner Syntax
Ruby1.9.1
String
No longer Enumerable
1.8 my_string_var.each { |line| puts line }
1.9 my_string_var.each_line { |line| puts line }
20. Cleaner Syntax
Ruby1.9.1
String
str = “test”
str.clear # => “”
“hellonworld”.lines # => [“hellon”, “world”]
“hello”.encoding # => “UTF-8”
“kitty”.start_with? “cat” # => false
“kitty”.end_with? “tty” # => true
22. Cleaner Syntax
Ruby1.9.1
Array
vowels = ['a','e','i', ‘o’, ‘u’]
vowels.index{|letter| letter == 'e'} # => 1
a = [1,2,3]
a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.combination(2).to_a #=> [[1, 2], [1, 3], [2, 3]]
a.to_s # => “[1, 2, 3]”
a.pop(2) #=> [2, 3]
27. Cleaner Syntax
Ruby1.9.1
Hash
Order Preservation
h = {:a => 1, :b => 2, :c => 3 }
h[:d] = 4
1.8 puts h.inspect => {:4 => d, :a => 1, :b => 2, :c => 3 }
28. Cleaner Syntax
Ruby1.9.1
Hash
Order Preservation
h = {:a => 1, :b => 2, :c => 3 }
h[:d] = 4
1.8 puts h.inspect => {:4 => d, :a => 1, :b => 2, :c => 3 }
1.9 puts h.inspect => {:a => 1, :b => 2, :c => 3, :4 => d }
31. Cleaner Syntax
Ruby1.9.1
Proc
New Declaration Syntax
1.8 say_hi = lambda { |a| “Hello, #{a}” }
1.9 say_hi = ->(a){ “Hello, #{a}” }
33. Cleaner Syntax
Ruby1.9.1
Proc
New Declaration Syntax
1.8 a = lambda { |x, y=1| x * y } #=> ERROR
1.9 a = ->(x, y=1){ x * y }
a = ->(&x){ x.call }
34. Cleaner Syntax
Ruby1.9.1
Proc
New Calling Syntax
say_hi = lambda { |a| “Hello, #{a}” }
1.8 say_hi.call(‘Quentin’) # => Hello, Quentin
35. Cleaner Syntax
Ruby1.9.1
Proc
New Calling Syntax
say_hi = lambda { |a| “Hello, #{a}” }
1.8 say_hi.call(‘Quentin’) # => Hello, Quentin
1.9 say_hi.(‘Quentin’) # => Hello, Quentin
38. Cleaner Syntax
Ruby1.9.1
Block Scope
n = “Hello, World!”
[1,2,3].each do |n|
#do something
end
1.8 puts n # => 3
1.9 puts n # => “Hello, World!”
41. Cleaner Syntax
Ruby1.9.1
Debugging
-w whitespace
Method#owner
#=> module the method belongs to
Method#source_location
#=> where the method is defined
42. Ruby1.9.1
Installation
http://gist.github.com/59130
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.bz2
tar -xjvf ruby-1.9.1-p0.tar.bz2
cd ruby-1.9.1-p0
./configure --prefix=/usr --program-suffix=19 --enable-shared
make && make install
44. Ruby1.9.1
Resources
•http://www.google.com