An Intro to Elegant Ruby

weather == 'raining' ? bring_an_umbrella : enjoy_the_sun
condition ? true : false
[*1..10]
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# You can even do multiple splats at once[*1..5, *'a'..'e']
#=> [1, 2, 3, 4, 5, "a", "b", "c", "d", "e"]
# If you're doing an array of letters you can also use 'character literals'# For example ?a == 'a'
so...
[*?a..?e]
#=> ["a", "b", "c", "d", "e"]
["You're" "probably" "doing" "a" "lot" "of" "this" "huh"]
%w[So instead you should definitely try this]# Did you know that what I wrote above returns what I wrote below?

#=>["So", "instead", "you", "should", "definitely", "try", "this"]
# Open up an irb and try it yourself
def songs
Songs.all.select { |song| song.artist == self }
end
def genres
songs.map { |song| song.genre}
end
def songs
Songs.all.select { |song| song.artist == self }
end
def genres
songs.map(&:genre)
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store