Send this to a friend
1 class Array
2 def to_quoted_s(q="'")
3 "#{q}#{self.join("#{q}, #{q}")}#{q}"
4 end
5 end
6
7
8
9 a = ['a', 'b', 'c', 'd', 'e']
10 puts a.to_s
11 puts a.to_quoted_s
12 puts a.to_quoted_s("\"")
class Array
def to_quoted_s(q="'")
"#{q}#{self.join("#{q}, #{q}")}#{q}"
end
end
a = ['a', 'b', 'c', 'd', 'e']
puts a.to_s
puts a.to_quoted_s
puts a.to_quoted_s("\"")
Send this to a friend
source: http://blog.carlosgabaldon.com/calabro/blog/post/2008/04/08/Ruby_Arrays
1
2
3 array = Array.new
4
5 array = []
6
7
8
9 array = []
10 array[0] = "first element"
11
12
13
14 array << "last element"
15
16
17 array.push("last element")
18
19
20
21
22 array = ["first element", "second element"]
23 array.unshift("before first element")
24
25
26
27
28
29
30 array = ["first element", "second element", "third element"]
31 third_element = array[2]
32
33
34
35
36
37
38
39
40
41 array = ["first element", "second element"]
42 first_element = array.shift
43
44
45
46 last_element = array.pop
47
48
49
50
51
52 array = [1, 2, 3]
53 array.concat([4, 5, 6, 7])
54
55 new_array = array + [4, 5, 6]
56
57
58
59
60 array.replace([4, 5, 6])
61
62
63 array = [1, 2, 3]
64 new_paired_array = array.zip([4, 5, 6])
65
66
67
68
69 array = [1, 2, 3]
70 new_paired_array_flattened = array.zip([4, 5, 6]).flatten
71
72
73
74
75
76 array = [1, 2, 3]
77 array.collect {|x| x * 2 }
78
79
80
81
82
83 [1, 2, 3, 4] .each {|x| puts x}
84
85
86
87
88
89
90 [1, 2, 3, 4, 5, 6] .find {|x| puts x > 5}
91
92
93
94
95 array.find_all {|item| criteria }
96
97
98 array.size
99
100
101 array.empty?
102
103
104 array.include?(item)
105
106
107 array.any? {|item| criteria }
108
109
110 array.all? {|item| criteria }
111
array = Array.new
array = []
array = []
array[0] = "first element"
array << "last element"
array.push("last element")
array = ["first element", "second element"]
array.unshift("before first element")
array = ["first element", "second element", "third element"]
third_element = array[2]
array = ["first element", "second element"]
first_element = array.shift
last_element = array.pop
array = [1, 2, 3]
array.concat([4, 5, 6, 7])
new_array = array + [4, 5, 6]
array.replace([4, 5, 6])
array = [1, 2, 3]
new_paired_array = array.zip([4, 5, 6])
array = [1, 2, 3]
new_paired_array_flattened = array.zip([4, 5, 6]).flatten
array = [1, 2, 3]
array.collect {|x| x * 2 }
[1, 2, 3, 4] .each {|x| puts x}
[1, 2, 3, 4, 5, 6] .find {|x| puts x > 5}
array.find_all {|item| criteria }
array.size
array.empty?
array.include?(item)
array.any? {|item| criteria }
array.all? {|item| criteria }
Send this to a friend
simple sample
1
2 orders = %w(10 11.5 1.5 24 100)
3
4
5 orders.map!{ |order| order.to_f }
6
7 total = orders.inject() {|result, element| result + element}
orders = %w(10 11.5 1.5 24 100)
orders.map!{ |order| order.to_f }
total = orders.inject() {|result, element| result + element}
Send this to a friend
Como criar um array usando ArrayObject e podendo acessar os valores como propriedade de um objeto
1 // acessando somente como array
2 $array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'));
3 echo 'Nome: ', $array['name'], "\n";
4 echo 'Usuário: ', $array['username'], "\n";
5
6 // acessando como objeto
7 $array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'), ArrayObject::ARRAY_AS_PROPS);
8 echo 'Nome: ', $array->name, "\n";
9 echo 'Usuário: ', $array->username, "\n";
10
11 // acessando de qualquer jeito
12 $array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'), ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
13 echo 'Nome: ', $array['name'], "\n";
14 echo 'Usuário: ', $array['username'], "\n";
15
16 echo 'Nome: ', $array->name, "\n";
17 echo 'Usuário: ', $array->username, "\n";
// acessando somente como array
$array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'));
echo 'Nome: ', $array['name'], "\n";
echo 'Usuário: ', $array['username'], "\n";
// acessando como objeto
$array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'), ArrayObject::ARRAY_AS_PROPS);
echo 'Nome: ', $array->name, "\n";
echo 'Usuário: ', $array->username, "\n";
// acessando de qualquer jeito
$array = new ArrayObject(array('name' => 'Rafael Souza', 'username' => 'rafaelss'), ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
echo 'Nome: ', $array['name'], "\n";
echo 'Usuário: ', $array['username'], "\n";
echo 'Nome: ', $array->name, "\n";
echo 'Usuário: ', $array->username, "\n";