-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.rb
69 lines (57 loc) · 1.51 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
68
69
# numbers = [4,8,15,16,23,42]
# toys = ["Teddy bear", "Super Soaker", "Board game"]
# things = [4, true, "Hello", 10.99]
# registrations = [true, true, false, true, false]
#
#
# students = [["Boris", 25, true],
# ["Sally", 23, true],
# ["Ingrid", 31, false]]
# names = %w[Jack Jill John James Jacob]
# puts names
# p Array.new(3, [1,2,3])
# fruits = ["Apple", "Orange", "Grape", "Banana"]
#
# p fruits[0]
# p fruits[1]
# p fruits[3]
# p fruits[]
# names = ["Tom", "Cameron", "Bob"]
#
# p names [2]
# p names[100]
#
# p names.fetch(2)
# p names.fetch(100, "N/A")
# numbers = [1,3,5,7,9,15,21]
# p numbers[2, 4]
# p numbers[1,3]
# p numbers[0,1]
# For arrays 3..6 means index 3 to index 6 unlike Ranges where it means 3,4,5,6
# numbers = [1,3,5,7,9,15,21,18,6]
# p numbers[3..6]
# p numbers[3...6]
# values_at lets you extract values in an array in any order such as 0, 5, 21 and the result is also an array
# channels = ["CBS", "FOX", "NBC", "ESPN", "UPN"]
# p channels.values_at(0, 2, 4)
# p channels.values_at(3,3,5,-3)
# Slice METHOD
# numbers=[0,2,4,8,10,12,14]
# p numbers.slice(3)
# p numbers.slice(100)
# p numbers.slice(2,3)
# p numbers.slice(4..6)
# p numbers.slice(4...6)
# Mutating arrays
# fruits = ["Apple", "Orange", "Grape", "Banana"]
# fruits[4] = "Raspberry"
# fruits[5]= "Strawberry"
# fruits[10]="Kiwi"
# fruits[3,2] = ["Canteloupe", "Dragonfruit"]
#
# fruits[0..2]=["Blackberry","Orange","Pears"]
# p fruits
#
# # fruits[0..3]= ["Blah"]
# # p fruits
# p fruits.slice(1..fruits.length-1)