-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.rb
67 lines (54 loc) · 1.14 KB
/
array.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
foo = ["foo", "bar", "nax", "pax"]
puts foo[1]
puts foo.include?("jar")
puts foo.include?("foo")
puts ""
puts "Calling reverse will not mutate the array"
p foo.reverse
p foo
puts "However using a bang(!) will mutate the array"
p foo.reverse!
p foo
puts ""
# To convert to an array
p (1..15).to_a
puts ""
# random array of 0-100
y = (0..10).to_a.shuffle
p y
x = (0..10).to_a.shuffle!
p x
puts ""
puts "Append to array"
x = (1..10).to_a
x << 15
p x
x.push(20)
p x
x.unshift(-1)
p x
puts ""
for number in x
puts number
end
puts ""
puts "using select works by giving it a boolean expression"
p x.select { |num| num.odd? }
puts "------------------------------------------------"
foo = %w[foobar1 foobar2].freeze
p foo
puts ""
# Looking into what Value equates to
foo.each do |key, value|
puts "key: #{key} value: #{value}"
end
puts ""
# Value is never set, so it's not needed checking to see if using just a key works
foo.each do |key|
puts "key: #{key}"
end
puts "------------------------------------------------"
# explodes a string
fruits = "apple, bannana, grapes, orange"
split_fruit = fruits.split(', ') # note to capture the space
puts split_fruit