forked from ren/ruby-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.rb
executable file
·28 lines (25 loc) · 857 Bytes
/
music.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
# Load the YAML ruby library.
require 'yaml'
# Read our file, gives us a hash that fulfills first challenge requirement.
# Ruby's YAML library does all the heavy lifting for us.
data = YAML.load_file 'music.yml'
# Modify Hash class to satisfy second challenge requirement.
# Essentially, if no method is defined passes argument to any hash as a value.
# So data.genres will yield the same output as data['genres'].
class Hash
def method_missing(meth)
self[meth.to_s]
end
end
# Just some debugging/tests.
#
# Output hash of YAML.
puts data.inspect
# Output genres.
puts data['genres']
# Output genres via method.
puts data.genres
# Output challenge requirement #1.
puts data['genres'].last['artists'].first['albums'].first['tracks'].last['name']
# Output challenge requirement #2.
puts data.genres.last.artists.first.albums.first.tracks.last.name