Created
July 11, 2012 23:53
-
-
Save octosteve/3094541 to your computer and use it in GitHub Desktop.
Silly Rubyness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 5 songs of the following lengths in seconds | |
class Song | |
attr_accessor :duration | |
def initialize(duration=0) | |
@duration = duration | |
end | |
end | |
# 223,215,378,324,254 | |
# Goals: | |
# Assign the lenght set to variables | |
songs = [] | |
songs << Song.new(223) | |
songs << Song.new(215) | |
songs << Song.new(378) | |
songs << Song.new(324) | |
songs << Song.new(254) | |
class Playlist | |
def initialize(songs=[]) | |
@songs = songs | |
end | |
# Calculate the Total Length of the Playlists | |
def length_in_seconds | |
@songs.map(&:duration).inject(:+) | |
end | |
# Express the Length in Minutes | |
def length_in_minutes | |
parsed_time = convert_seconds_to_minutes(length_in_seconds) | |
pretty_output parsed_time | |
end | |
# Average Song Length in Minutes | |
def average_length_in_minutes | |
average_in_seconds = length_in_seconds / @songs.count | |
parsed_time = convert_seconds_to_minutes(average_in_seconds) | |
pretty_output parsed_time | |
end | |
# Add a 2 Second Gap In Between Songs | |
def length_with_gap(gap=2) | |
total_pause_length = (@songs.count * gap) - gap | |
length_in_seconds + total_pause_length | |
end | |
private | |
def convert_seconds_to_minutes(seconds) | |
time = {} | |
time[:minutes] = seconds / 60 | |
time[:seconds] = seconds % 60 | |
time | |
end | |
def pretty_output parsed_time | |
"#{parsed_time[:minutes]} minutes and #{parsed_time[:seconds]} seconds" | |
end | |
end | |
playlist = Playlist.new(songs) | |
puts "Current total length of the playlist in seconds: #{playlist.length_in_seconds}" | |
puts "Total length in minutes: #{playlist.length_in_minutes}" | |
puts "Average song length in minutes: #{playlist.average_length_in_minutes}" | |
puts "Total songs length with 2 second gap in seconds: #{playlist.length_with_gap(2)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment