-
Notifications
You must be signed in to change notification settings - Fork 0
/
L1-hash_and_arrays.rb
54 lines (37 loc) · 1.14 KB
/
L1-hash_and_arrays.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
# Create a hash
track = {:name => "Electic Worm",
:artist => "Beastie Boys",
:track_number => 6
}
# Notice the variables are not typed
# :name is a symbol - constants that are guaranteed to be unique
#access hash
track[:artist]
#check class
track[:artist].class
#change track number
track[:track_number] += 1
#create a method
def show_track(track)
puts "Track #{track[:artist]} - #{track[:name]}"
end
#call method
show_track(track)
#create an array
playlist = []
#add our track
playlist << track
#let's add a couple more
playlist << {:name => 'Hurricane', :artist => 'Bob Dylan' }
playlist << {:name => 'Sabotage', :artist => "Beastie Boys"}
playlist << {:name => 'Home', :artist => 'Edward Sharpe' }
playlist << {:name => 'Right Here, Right Now', :artist => 'Fatboy Slim'}
#check the content of playlist
puts playlist
#first track in playlist - you do not need parenthesis around parameters
show_track playlist[0]
#list all of the names of the songs in our playlist
playlist.collect {|t| t[:name]}
#all of the beastie boy songs
playlist.select {|t| t[:artist] == "Beastie Boys"}
#Methods on array - http://www.ruby-doc.org/core-1.9.3/Array.html