-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3fbdec3
Showing
10 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mister = "fuck" | ||
|
||
5.times do |n; mister| | ||
mister = "John" | ||
puts "#{n} situp, #{mister}" | ||
end | ||
|
||
puts mister |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cards = ["Jack", "Queen", "King", "Ace", "Joker"] | ||
|
||
scores = {"Larry" => 10, "Moe" => 8, "Curly" => 12} | ||
|
||
desserts = { "chocolate" => 1.00, "vanilla" => 0.75, "cinnamon" => 1.25 } | ||
|
||
#cards.shuffle.each { |card| puts "#{card.upcase} - #{card.length}" } | ||
|
||
#cards.reverse_each { |card| puts "#{card.upcase} - #{card.length}" } | ||
|
||
#scores.each { |name, score| puts "#{name} scores a #{score}" } | ||
|
||
desserts.each { |type, price| puts "$#{price*2} for a cup of #{type}." } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def deal(num_of_cards) | ||
faces = ["Jack", "Queen", "King", "Ace"] | ||
suits = ["Hearts", "Diamonds", "Spades", "Clubs"] | ||
# call the block here | ||
if block_given? | ||
num_of_cards.times do | ||
random_face = faces.sample | ||
random_suit = suits.sample | ||
result = yield(random_face, random_suit) | ||
puts "You scored a #{result}" | ||
end | ||
else | ||
puts "No deal" | ||
end | ||
end | ||
|
||
deal(5) do |face, suit| | ||
puts "Dealt a #{face} of #{suit}" | ||
face.length + suit.length | ||
end | ||
|
||
|
||
|
||
def progress | ||
0.step(100, 10) { |x| yield(x) } | ||
end | ||
|
||
progress { |percent| puts percent } | ||
|
||
|
||
def greet | ||
yield("Fuck", "18", "23", 25) | ||
end | ||
|
||
greet { |name, age| puts "Hello, #{name}. You don't look #{age}!" } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class Flyer | ||
attr_reader :name, :email, :miles_flown | ||
attr_accessor :status | ||
|
||
def initialize(name, email, miles_flown, status=:bronze) | ||
@name = name | ||
@email = email | ||
@miles_flown = miles_flown | ||
@status = status | ||
end | ||
|
||
def to_s | ||
"#{name} (#{email}): #{miles_flown} - #{status}" | ||
end | ||
end | ||
|
||
flyers = [] | ||
flyers << Flyer.new("Larry", "[email protected]", 4000, :platinum) | ||
flyers << Flyer.new("Moe", "[email protected]", 1000) | ||
flyers << Flyer.new("Curly", "[email protected]", 3000, :gold) | ||
flyers << Flyer.new("Shemp", "[email protected]", 2000) | ||
|
||
|
||
# frequent_flyers = flyers.select { |x| x.miles_flown >= 3000 } | ||
# puts frequent_flyers | ||
|
||
# low_frequent_flyers = flyers.reject { |x| x.miles_flown >= 3000 } | ||
# puts low_frequent_flyers | ||
|
||
# puts flyers.any? { |x| x.status == :platinum } | ||
|
||
# puts flyers.detect { |x| x.status == :bronze } | ||
|
||
# platinum, powerty = flyers.partition { |x| x.status == :platinum} | ||
# p platinum | ||
# p powerty | ||
|
||
# tags = flyers.map { |x| "#{x.name} (#{x.status.upcase})" } | ||
# puts tags | ||
|
||
# kilometers = flyers.map { |x| x.miles_flown * 1.6 } | ||
# puts kilometers | ||
|
||
# total_miles = flyers.map { |x| x.miles_flown }.reduce(0, :+) | ||
# puts total_miles | ||
|
||
# total_kilometers = flyers.map { |x| x.miles_flown * 1.6 }.reduce(0, :+) | ||
# puts total_kilometers | ||
|
||
total_bronze_kilo = flyers.select { |x| x.status == :bronze }.map { |x| x.miles_flown * 1.6 }.inject(:+) | ||
puts total_bronze_kilo | ||
|
||
big_miles = flyers.max_by { |x| x.miles_flown } | ||
puts big_miles |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Flyer | ||
attr_reader :name, :email, :miles_flown | ||
|
||
def initialize(name, email, miles_flown) | ||
@name = name | ||
@email = email | ||
@miles_flown = miles_flown | ||
end | ||
|
||
def to_s | ||
"#{name} (#{email}): #{miles_flown}" | ||
end | ||
end | ||
|
||
flyers = [] | ||
1.upto(5) do |x| | ||
flyers << Flyer.new("Flyer #{x}", "flyer#{x}@email.com", x * 1000) | ||
end | ||
|
||
# Name - how mush miles | ||
flyers.each { |x| puts "#{x.name} - #{x.miles_flown} miles" } | ||
|
||
#Total miles | ||
total = 0 | ||
flyers.each { |x| total += x.miles_flown } | ||
puts "Total miles flown: #{total}" | ||
|
||
|
||
# Bonus miles promotions | ||
promotions = { "United" => 1.5, "Delta" => 2.0, "Lufthansa" => 2.5 } | ||
|
||
promotions.each { |name, bonus| puts "Earn #{bonus}x miles by flying #{name}!" } | ||
|
||
# Combine two iterators to print out the miles each flyer would | ||
# earn by switching to each airline | ||
flyers.each do |x| | ||
promotions.each do |name, bonus| | ||
puts "#{x.name} could earn #{x.miles_flown * bonus} miles by flying #{name}!" | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def n_times(n) | ||
1.upto(n) { |x| yield(x) } | ||
end | ||
|
||
n_times(5) do |number| | ||
puts "#{number} situp" | ||
puts "#{number} pushup" | ||
puts "#{number} chinup" | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'mathn' | ||
|
||
Prime.each { |number| puts "The next prime is #{number}" } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module MyEnumerable | ||
def my_map | ||
new_array = [] | ||
each do |value| | ||
new_array << yield(value) | ||
end | ||
new_array | ||
end | ||
|
||
def my_select | ||
new_array = [] | ||
each do |value| | ||
new_array << value if yield(value) | ||
end | ||
new_array | ||
end | ||
|
||
def my_reject | ||
new_array = [] | ||
each do |value| | ||
new_array << value unless yield(value) | ||
end | ||
new_array | ||
end | ||
|
||
def my_detect | ||
each do |value| | ||
return value if yield(value) | ||
end | ||
nil | ||
end | ||
|
||
def my_any? | ||
each do |value| | ||
return true if yield(value) | ||
end | ||
false | ||
end | ||
|
||
def my_reduce(initial_value) | ||
sum = initial_value | ||
each do |value| | ||
sum = yield(sum, value) | ||
end | ||
sum | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
scores = [83, 71, 92, 64, 98, 87, 75, 69] | ||
|
||
# ALLOWED_TYPES = [:celsium, :farengheit, :kelvin] | ||
|
||
# high_scores = scores.select { |x| x > 80 } | ||
# puts high_scores | ||
|
||
# low_scores = scores.reject { |x| x > 80 } | ||
# puts low_scores | ||
|
||
|
||
# type = :celsium | ||
# outtype = :farengheit | ||
|
||
#ALLOWED_TYPES.include?(type) and ALLOWED_TYPES.include?(outtype) | ||
# puts [type, outtype].all? { |x| ALLOWED_TYPES.include? x } | ||
|
||
# puts scores.any? { |score| score < 70 } | ||
|
||
# puts scores.any? { |score| score < 70 } | ||
|
||
# puts scores.detect { |score| score < 70 } | ||
|
||
# scores.select! { |x| x > 70 } | ||
# p scores | ||
|
||
# scores.reject! { |x| x.even? } | ||
# p scores | ||
|
||
# scores_doubled = scores.map { |x| x * 2 } | ||
# puts scores_doubled | ||
|
||
# total = scores.reduce(0, :+) | ||
# puts total | ||
|
||
# evens, odds = scores.partition { |x| x.even? } | ||
# p evens | ||
# p odds |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
require_relative 'my_enumerable' | ||
|
||
class Song | ||
attr_reader :name, :artist, :duration | ||
|
||
def initialize(name, artist, duration) | ||
@name = name | ||
@artist = artist | ||
@duration = duration | ||
end | ||
|
||
def play | ||
puts "Playing '#{name}' by #{artist} (#{duration} mins)..." | ||
end | ||
|
||
def each_filename | ||
basename = "#{name}-#{artist}".gsub(" ", "-").downcase | ||
extensions = [".mp3", ".wav", ".ac"] | ||
extensions.each { |ext| yield basename + ext } | ||
end | ||
end | ||
|
||
song1 = Song.new("Where is my mind", "The dust brothers", "4") | ||
song2 = Song.new("The man comes around", "Jonny Cash", "5") | ||
song3 = Song.new("Radioactive", "Imagine Dragons", "7") | ||
song4 = Song.new("Somewhere I belong", "Linkin park", "8") | ||
|
||
class Playlist | ||
#include Enumerable | ||
include MyEnumerable | ||
|
||
def initialize(name) | ||
@name = name | ||
@songs = [] | ||
end | ||
|
||
def add_song(song) | ||
@songs << song | ||
end | ||
|
||
def each | ||
@songs.each do |song| | ||
yield song | ||
end | ||
end | ||
|
||
def each_tagline | ||
@songs.each { |song| yield "#{song.name} - #{song.artist}" } | ||
end | ||
|
||
def each_by_artist(artist) | ||
select { |song| song.artist == artist }.each { |song| yield song } | ||
end | ||
end | ||
|
||
playlist = Playlist.new("Favorite") | ||
playlist.add_song(song1) | ||
playlist.add_song(song2) | ||
playlist.add_song(song3) | ||
playlist.add_song(song4) | ||
|
||
|
||
#playlist.each { |song| song.play } | ||
|
||
# mind_songs = playlist.my_select { |song| song.name =~ /mind/ } | ||
# p mind_songs | ||
|
||
# short = playlist.detect { |song| song.duration.to_i < 6 } | ||
# p short | ||
|
||
# dragons = playlist.any? { |song| song.artist =~ /Dragons/ } | ||
# p dragons | ||
|
||
# longest = playlist.inject(0) do |sum, song| | ||
# puts "Yielding #{sum}" | ||
# sum > song.duration.to_i ? sum : song.duration.to_i | ||
# end | ||
# puts longest | ||
|
||
|
||
# song_labels = playlist.my_map { |song| "#{song.name} - #{song.artist}" } | ||
# p song_labels | ||
|
||
# song_labels = playlist.my_reject { |song| song.name =~ /mind/ } | ||
# p song_labels | ||
|
||
|
||
# total_duration = playlist.reduce(0) { |sum, song| sum + song.duration.to_i } | ||
# p total_duration | ||
|
||
# playlist.each_tagline { |tagline| puts tagline } | ||
|
||
|
||
# playlist.each_by_artist("Linkin park") { |song| song.play } | ||
|
||
# song1.each_filename { |filename| puts filename } | ||
|
||
# p playlist.my_detect { |song| song.artist == "Linkin park" } | ||
|
||
# p playlist.my_any? { |song| song.artist == "Linkin park" } | ||
|
||
total_duration = playlist.my_reduce(0) { |sum, song| sum + song.duration.to_i } | ||
p total_duration |