Ruby Data Structures: Difference between revisions
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
* "abc": Double-quoted strings allow substitution and backslash notation. | * "abc": Double-quoted strings allow substitution and backslash notation. | ||
* 'abc': Single-quoted strings don't allow substitution and allow backslash notation only for \\ and \'. | * 'abc': Single-quoted strings don't allow substitution and allow backslash notation only for \\ and \'. | ||
* alternate string literals with <code>%q</code> (equivalent to single quote) and <code>%Q</code> (equivalent to double quote): | |||
<source lang="ruby"> | |||
s1 = %q[As Magritte said, "Ceci n'est pas une pipe."] | |||
s2 = %q*\r is a control-M and \n is a control-J.* | |||
</source> | |||
* Here document: create a multi-line string, allowing substitution: | * Here document: create a multi-line string, allowing substitution: | ||
<source lang="ruby"> | <source lang="ruby"> |
Revision as of 21:38, 5 October 2011
Numbers
123 # integer of type Fixnum
12345678901234567890 # Bignum: an integer of arbitrary length
2e-20 # Float
?a # character code for 'a', which is 97
Strings
- "abc": Double-quoted strings allow substitution and backslash notation.
- 'abc': Single-quoted strings don't allow substitution and allow backslash notation only for \\ and \'.
- alternate string literals with
%q
(equivalent to single quote) and%Q
(equivalent to double quote):
s1 = %q[As Magritte said, "Ceci n'est pas une pipe."]
s2 = %q*\r is a control-M and \n is a control-J.*
- Here document: create a multi-line string, allowing substitution:
name = "Bill"
letter = <<letter
Dear #{name},
I got your letter!
Sincerely,
Scott
letter
Concatenation:
"foo" "bar" # means "foobar"
"foo" + "bar" # means "foobar"
"foo" << "bar" # means "foobar"
Substitution:
"The value is #{ some expression }." # substitute the value of an arbitrary expression
"My name is #{$name}." # substitute the value of a variable
"My name is #$name." # shortcut for the same thing
Substring:
x = "abcdefg"
x[2..4] # "cde" (chars 2 through 4 inclusive)
x[2...4] # "cd" (chars 2 and 3)
x[2,4] # "cdef" (extract a string of length 4)
x[2] # 99 (ascii code for "c")
x[2].chr # "c"
x["bc"] # "bc"
x["cb"] # nil
x[/bc/] # "bc"
x.include? "bc" # true
Split:
"a b c".split # ["a", "b", "c"]
"a, b, c".split(/, /) # ["a", "b", "c"]
Methods:
x = "abcdefg"
x.size # 7
x.length # 7
x.empty? # false
x.include? "c" # true
x.upcase # "ABCDEFG"
x.gsub("a", "A") # "Abcdefg"
Arrays
Create
x = [1, 2, 3]
x = %w(foo bar baz) # ["foo", "bar", "baz"]
Concatenate
[1, 2, 3] << 4 # [1, 2, 3, 4]
Iterate
my_array.each do | element |
do something with element
end
or
my_array.each do |e| { do something with e }
Methods
x = ["a", "b", "c"]
x.length # 3
x.first # "a"
x.last # "c"
x.last(2) # ["b", "c"]
x[0] # "a"
x[0,2] # ["a", "b"]
x[0..2] # ["a", "b", "c"]
x.index("a") # 0
x.include? "a" # true
x << "d" # ["a", "b", "c", "d"]
[1, 2, 1].uniq # [1, 2]
x.pop # "c", x is ["a", "b"]
x.push("d") # ["a", "b", "c", "d"]
x.delete("b") # returns "b", leaves x == ["a", "c"]
x.join # "abc"
x.join(", ") # "a, b, c"
x.map{|e| e.upcase} # ["A", "B", "C"]
x.reverse # ["c", "b", "a"]
x.reverse.sort # ["a", "b", "c"]
x.sort_by! {|a| a.name} # sort on name attribute
Set operations
x = ["a", "b", "c"]
y = ["b", "c", "d"]
x & y # ["b", "c"]
x - y # ["a"]
x | y # ["a", "b", "c", "d"]
Range operators
5..10
includes the number 10, but 5...10
does not.
Do not confuse ranges with arrays. These two assignments are entirely different:
x = 1..5
x = [1, 2, 3, 4, 5]
However, there is a convenient method to_a
for converting ranges to arrays.
Array expansion
Also called the "splat" operator
x = [1, 2, 3]
a, b, c = *x # now a is 1, b is 2, c is 3
Hashes
Iterate:
x = {:a => 1, :b => 2, :c => 3}
x.each{|k,v| print "(#{k}, #{v})"} # (b, 2)(c, 3)(a, 1)
x.each do |k,v|
...do something with k, v
end
x.each_key{|k| print k} # bca
x.each_value{|v| print v} # 231
Methods
x = {:a => 1, :b => 2, :c => 3}
x.key? :b # true
x.keys # [:b, :c, :a]
x.value? 2 # true
x.values # [2, 3, 1]
x[:a] # 1
x.delete :c # x is {:b=>2, :a=>1}
x = {:b=>2, :a=>1}
y = {:b => 22, :c => 33}
x.merge y # {:b=>22, :c=>33, :a=>1}
x.clear # {}
x.empty? # true
Date and Time
What time is it now?
now = Time.now # Wed Sep 12 11:51:41 -0700 2007
now.year # 2007
now.month # 9
now.day # 12
now.wday # 3
now.hour # 11
now.min # 51
now.sec # 41
Create an arbitrary date:
my_time = Time.local(2007, 9, 11, 17, 30, 13) # Tue Sep 11 17:30:13 -0700 2007
Date-time arithmetic:
diff = now - my_time # 66088.978569 (in seconds)
diff/(60*60) # 18.3580496025 (in hours)
now + 24*60*60 # Thu Sep 13 11:51:41 -0700 2007
Date-only:
require 'date'
today = Date.today
today.to_s # "2007-09-12"
yesterday = Date.new(2007, 9, 11)
yesterday.to_s # "2007-09-11"
today - yesterday # Rational(1, 1)
tomorrow = today + 1
tomorrow.to_s # "2007-09-13"