my-flicks 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/flicks +2 -2
- data/bin/flicks.rb +2 -2
- data/lib/flicks/blocks.rb +66 -0
- data/lib/flicks/conditionals.rb +23 -0
- data/lib/flicks/file.rb +37 -0
- data/lib/flicks/fliks_bis.rb +15 -0
- data/lib/flicks/hash.rb +47 -0
- data/lib/flicks/iterators.rb +80 -0
- data/lib/flicks/mixins.rb +44 -0
- data/lib/flicks/movie.rb +98 -0
- data/lib/flicks/movie3d.rb +44 -0
- data/lib/flicks/namespaces.rb +32 -0
- data/lib/flicks/playlist.rb +90 -0
- data/lib/flicks/rankable.rb +33 -0
- data/lib/flicks/snack_bar.rb +48 -0
- data/lib/flicks/sorting.rb +28 -0
- data/lib/flicks/symbols.rb +23 -0
- data/lib/flicks/waldorf_and_statler.rb +28 -0
- metadata +19 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2d68521c3ef91a07ca7576b50089090db1977d1a
|
|
4
|
+
data.tar.gz: 9614adbba7e220d4db2f922fa2262dd59100659c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ef646d3e3322712997eba4b39f2bd6b6368ce7f4abd2b1b9d3c94ad44b3eae384f2b7c94f0a246a567dac4faf10deb42335b5004d2acdb7bf4e4da57c88a3271
|
|
7
|
+
data.tar.gz: 8096b723e4dc9fba53c6138b73a7863ebda9235d5c49578f376a99c5c75629842bc390d7325d5ac847731c91fcbb16b739e8de28459e76dec62c1efda3f5475c
|
data/bin/flicks
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
# this is the main ruby file, thats why it doesnt have .rb extension at the end
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
require_relative '../lib/flicks/playlist'
|
|
8
|
+
require_relative '../lib/flicks/movie3d'
|
|
9
9
|
|
|
10
10
|
# movie1 = Movie.new('goonies',10)
|
|
11
11
|
# movie2 = Movie.new('up', 5)
|
data/bin/flicks.rb
CHANGED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
3.times { puts "Echo" } # single line block
|
|
2
|
+
|
|
3
|
+
# multiple line block srrounded by do .. end
|
|
4
|
+
# 10.times do
|
|
5
|
+
# puts 'situp'
|
|
6
|
+
# puts 'pushup'
|
|
7
|
+
# puts 'chinup'
|
|
8
|
+
# end
|
|
9
|
+
|
|
10
|
+
# block can take parameters eg. |number| will catch the iteration number
|
|
11
|
+
# number will start from 0 up to 9
|
|
12
|
+
# 10.times do |number|
|
|
13
|
+
# puts "#{number} situp"
|
|
14
|
+
# puts "#{number} pushup"
|
|
15
|
+
# puts "#{number} chinup"
|
|
16
|
+
# end
|
|
17
|
+
|
|
18
|
+
# will start from 1 up to 10
|
|
19
|
+
1.upto(10) do |number|
|
|
20
|
+
puts "#{number} situp"
|
|
21
|
+
puts "#{number} pushup"
|
|
22
|
+
puts "#{number} chinup"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# arrays
|
|
26
|
+
numbers = [1, 2, 3, 4]
|
|
27
|
+
|
|
28
|
+
# to_a will convert the range to an array
|
|
29
|
+
numbers = (1..10).to_a
|
|
30
|
+
|
|
31
|
+
# iterate n and select numbers that are > 5
|
|
32
|
+
numbers.select { |n| n > 5 }
|
|
33
|
+
|
|
34
|
+
# if multiple lines
|
|
35
|
+
numbers.select do |n|
|
|
36
|
+
n > 5
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
evens = numbers.select { |n| n.even? }
|
|
40
|
+
odds = numbers.select { |n| n.odd? }
|
|
41
|
+
# or by rejecting all the even numbers
|
|
42
|
+
odds = numbers.reject { |n| n.even? }
|
|
43
|
+
# to partition
|
|
44
|
+
evens, odds = numbers.partition { |n| n.even? } #returns a nested arraythat ruby will partition : [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]] in evens and odds
|
|
45
|
+
# to reduce : it takes 2 parameters |param1 , param2|
|
|
46
|
+
# param1: the accumulator, will take what s inside the block in the last iteration
|
|
47
|
+
# param2: the actual iterator
|
|
48
|
+
numbers.reduce { |sum, n| sum + n }
|
|
49
|
+
# reduce can take a symble of an operator to apply to all the elements of the array
|
|
50
|
+
numbers.reduce(:+)
|
|
51
|
+
numbers.reduce(:*)
|
|
52
|
+
|
|
53
|
+
# sort
|
|
54
|
+
numbers.sort
|
|
55
|
+
|
|
56
|
+
numbers.sort.reverse
|
|
57
|
+
# or
|
|
58
|
+
numbers.sort { |a, b| b <=> a }
|
|
59
|
+
|
|
60
|
+
# Check if any? even number in the list
|
|
61
|
+
numbers.any? { |n| n.even? }
|
|
62
|
+
numbers.detect { |n| n.even? }
|
|
63
|
+
|
|
64
|
+
# Multiply all the numbers in the list by 2
|
|
65
|
+
# nmbers will be untouched
|
|
66
|
+
numbers_doubled = numbers.map { |number| number * 2 }
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'flicks/movie'
|
|
2
|
+
|
|
3
|
+
movie = Movie.new('godfather',10)
|
|
4
|
+
|
|
5
|
+
# puts movie.rank == 10
|
|
6
|
+
# puts movie.rank >= 10
|
|
7
|
+
# puts movie.rank < 10
|
|
8
|
+
|
|
9
|
+
# if movie.rank >= 10
|
|
10
|
+
# puts 'Hit'
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
# puts 'Hit' if movie.rank >= 10
|
|
14
|
+
|
|
15
|
+
# if movie.rank < 10
|
|
16
|
+
# puts 'Flop'
|
|
17
|
+
# end
|
|
18
|
+
|
|
19
|
+
if movie.rank < 10
|
|
20
|
+
puts 'Flop'
|
|
21
|
+
else
|
|
22
|
+
puts 'Hit'
|
|
23
|
+
end
|
data/lib/flicks/file.rb
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# To open a file in Ruby we use File.open('filename') method that returns a file object
|
|
2
|
+
|
|
3
|
+
# file = File.open('movies.rb')
|
|
4
|
+
# # Read file
|
|
5
|
+
# file.close
|
|
6
|
+
|
|
7
|
+
# Someties we forget to close the file, so we can also do
|
|
8
|
+
# The block will close the file when we are done
|
|
9
|
+
|
|
10
|
+
# File.open('movies.csv') do |file|
|
|
11
|
+
# file.each_line do |line|
|
|
12
|
+
# puts line
|
|
13
|
+
# end
|
|
14
|
+
# end
|
|
15
|
+
|
|
16
|
+
# or simply using readlines method that returns an array
|
|
17
|
+
|
|
18
|
+
# File.readlines('movies.csv').each do |line|
|
|
19
|
+
# puts line
|
|
20
|
+
# end
|
|
21
|
+
|
|
22
|
+
# line = "Gonies,10"
|
|
23
|
+
# fields = line.split(',') # This will return an array
|
|
24
|
+
# puts fields
|
|
25
|
+
# puts fields[0]
|
|
26
|
+
# puts fields[1]
|
|
27
|
+
# title, rank = line.split(',')
|
|
28
|
+
# puts title
|
|
29
|
+
# puts rank
|
|
30
|
+
|
|
31
|
+
require_relative 'movie'
|
|
32
|
+
|
|
33
|
+
File.readlines('movies.csv').each do |line|
|
|
34
|
+
title, rank = line.split(',')
|
|
35
|
+
movie = Movie.new(title, rank.to_i)
|
|
36
|
+
puts movie
|
|
37
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'flicks/playlist'
|
|
2
|
+
|
|
3
|
+
movie1 = Movie.new('goonies',10)
|
|
4
|
+
movie2 = Movie.new('up', 5)
|
|
5
|
+
movie3 = Movie.new('scarface')
|
|
6
|
+
|
|
7
|
+
movies = [movie1, movie2, movie3]
|
|
8
|
+
|
|
9
|
+
# Open file in write mode
|
|
10
|
+
File.open('movie_rankings.csv', 'w') do |file|
|
|
11
|
+
movies.sort.each do |movie|
|
|
12
|
+
file.puts "#{movie.title},#{movie.rank}"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
data/lib/flicks/hash.rb
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
snack_carbs = {:candy => 15, :pretzel => 10, :soda => 5}
|
|
2
|
+
puts snack_carbs[:candy]
|
|
3
|
+
|
|
4
|
+
# in ruby 9.1
|
|
5
|
+
new_snack_carbs = {candy: 30, pretzel: 10, soda: 5}
|
|
6
|
+
puts new_snack_carbs[:pretzel]
|
|
7
|
+
|
|
8
|
+
# Create empty hash
|
|
9
|
+
snack_carbs = {}
|
|
10
|
+
# or
|
|
11
|
+
snack_carbs = Hash.new
|
|
12
|
+
|
|
13
|
+
# assign a value to the key candy
|
|
14
|
+
snack_carbs[:candy] = 15
|
|
15
|
+
|
|
16
|
+
snack_carbs = {:candy => 15, :pretzel => 10, :soda => 5}
|
|
17
|
+
# Return all the keys # return an array
|
|
18
|
+
snack_carbs.keys
|
|
19
|
+
# Return all the values # return an arrays
|
|
20
|
+
puts snack_carbs.values
|
|
21
|
+
|
|
22
|
+
# Iteration on hashes # below name iterate on keys, carbs iterate on values
|
|
23
|
+
snack_carbs.each do |name, carbs|
|
|
24
|
+
puts "#{name} has #{carbs} carbs"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Accumulate in a hash
|
|
28
|
+
snack_carbs[:candy] = 15
|
|
29
|
+
snack_carbs[:candy] += 15
|
|
30
|
+
# now snack_carbs[:candy] has 30
|
|
31
|
+
# but if snack_carbs doesnt have key coke
|
|
32
|
+
#snack_carbs[:coke] # has nil in it
|
|
33
|
+
# we cant iterate unless it has initial value
|
|
34
|
+
#snack_carbs[:coke] += 30 # return error
|
|
35
|
+
|
|
36
|
+
# specify default value in a hash (to avoid the nil scenario)
|
|
37
|
+
snack_carbs = Hash.new(0)
|
|
38
|
+
puts snack_carbs[:coke]
|
|
39
|
+
snack_carbs[:coke] += 30
|
|
40
|
+
puts snack_carbs[:coke]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
carbs [10, 20, 30]
|
|
44
|
+
carbs.reduce { |sum, n| sum + n }
|
|
45
|
+
carbs.reduce(:+)
|
|
46
|
+
# if the carbs array is empty
|
|
47
|
+
carbs.reduce(0, :+)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# def once
|
|
2
|
+
# puts "Before yield"
|
|
3
|
+
# yield ## Will be replace by the block {} below when calling once
|
|
4
|
+
# puts "After yield"
|
|
5
|
+
# end
|
|
6
|
+
|
|
7
|
+
# once { puts "Running your block" }
|
|
8
|
+
|
|
9
|
+
# # Before yield
|
|
10
|
+
# # Running your block
|
|
11
|
+
# # After yield
|
|
12
|
+
|
|
13
|
+
# def twice
|
|
14
|
+
# puts "Before yield"
|
|
15
|
+
# yield
|
|
16
|
+
# yield
|
|
17
|
+
# puts "After yield"
|
|
18
|
+
# end
|
|
19
|
+
|
|
20
|
+
# twice {puts "Running your block"}
|
|
21
|
+
# # Before yield
|
|
22
|
+
# # Running your block
|
|
23
|
+
# # Running your block
|
|
24
|
+
# # After yield
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# yield with parameters
|
|
28
|
+
# def three_times
|
|
29
|
+
# puts 'Ready'
|
|
30
|
+
# yield(1) # returns the first iteration of three_times {} below
|
|
31
|
+
# puts 'Set'
|
|
32
|
+
# yield 2 # () are optional in Ruby
|
|
33
|
+
# puts 'Go!'
|
|
34
|
+
# yield(3) # returns the third iteration of three_times {} below
|
|
35
|
+
# end
|
|
36
|
+
|
|
37
|
+
# three_times { |number| puts number }
|
|
38
|
+
|
|
39
|
+
# def compute
|
|
40
|
+
# puts yield
|
|
41
|
+
# end
|
|
42
|
+
|
|
43
|
+
# compute do
|
|
44
|
+
# 'Hello'
|
|
45
|
+
# 7
|
|
46
|
+
# 3.14 # This once will be printed because it is the last statement and will be returned from this block
|
|
47
|
+
# end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# def compute
|
|
52
|
+
# puts yield
|
|
53
|
+
# end
|
|
54
|
+
|
|
55
|
+
# compute # this will cause an error because compute is not a block here and returns nothing to yield #to get around this we can do
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# def compute
|
|
59
|
+
# if block_given? # this is a build in Ruby method
|
|
60
|
+
# puts yield
|
|
61
|
+
# else
|
|
62
|
+
# puts 'does not compute'
|
|
63
|
+
# end
|
|
64
|
+
# end
|
|
65
|
+
|
|
66
|
+
# compute
|
|
67
|
+
# compute { 3.14 }
|
|
68
|
+
|
|
69
|
+
numbers = (1..10).to_a
|
|
70
|
+
#puts numbers.select { |n| n.even? }
|
|
71
|
+
|
|
72
|
+
def my_select(array)
|
|
73
|
+
results = []
|
|
74
|
+
array.each do |element|
|
|
75
|
+
results << element if yield(element)
|
|
76
|
+
end
|
|
77
|
+
results
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
puts my_select(numbers) { |n| n.even? }
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module Rankable
|
|
2
|
+
|
|
3
|
+
# We cannot create an instance of the module Rankable, so we cannot call this method directly -> S we mixed in the classes below
|
|
4
|
+
def thumbs_up
|
|
5
|
+
puts "#{@title} got a thumbs up!" # notice that it can call @title when it gets mixed in
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class Movie
|
|
11
|
+
include Rankable
|
|
12
|
+
|
|
13
|
+
def initialize(title, rank)
|
|
14
|
+
@title = title
|
|
15
|
+
@rank = rank
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Song
|
|
21
|
+
|
|
22
|
+
include Rankable # we mix in the module Rankable
|
|
23
|
+
|
|
24
|
+
def initialize(title, rank)
|
|
25
|
+
@title = title
|
|
26
|
+
@rank = rank
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
movie = Movie.new("Goonies", 10)
|
|
32
|
+
movie.thumbs_up
|
|
33
|
+
|
|
34
|
+
song = Song.new("Ruby Baby", 10)
|
|
35
|
+
song.thumbs_up
|
|
36
|
+
|
|
37
|
+
# Note :
|
|
38
|
+
# in irb
|
|
39
|
+
# load 'mixins.rb'
|
|
40
|
+
# Movie.ancestors
|
|
41
|
+
# => [Movie, Rankable, Object, Kernel, BasicObject] # Notice Rankable here between Movie and Object
|
|
42
|
+
# Song.ancestors
|
|
43
|
+
# => [Song, Rankable, Object, Kernel, BasicObject]
|
|
44
|
+
|
data/lib/flicks/movie.rb
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
require 'flicks/rankable'
|
|
2
|
+
|
|
3
|
+
class Movie
|
|
4
|
+
include Rankable
|
|
5
|
+
|
|
6
|
+
# attr_reader :rank # changed to accessor attribute because the module Rankable needs to edit it
|
|
7
|
+
attr_accessor :rank
|
|
8
|
+
attr_accessor :title
|
|
9
|
+
|
|
10
|
+
def initialize(title, rank = 0)
|
|
11
|
+
@title = title.capitalize
|
|
12
|
+
@rank = rank
|
|
13
|
+
@snack_carbs = Hash.new(0)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# We dont have an object here to apply from csv to, so we apply it on self ie. the class itself
|
|
17
|
+
def self.from_csv(line)
|
|
18
|
+
title, rank = line.split(',')
|
|
19
|
+
# Integer(rank) instead of rank.to_i in order to raise an exception if the rank could not be parsed to int, using to_i will return 0
|
|
20
|
+
Movie.new(title, Integer(rank))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def ate_snack(snack)
|
|
24
|
+
@snack_carbs[snack.name] += snack.carbs
|
|
25
|
+
puts "#{@title} led to #{snack.carbs} #{snack.name} carbs being consumed."
|
|
26
|
+
puts "#{@title}'s snacks: #{@snack_carbs}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def carbs_consumed
|
|
30
|
+
@snack_carbs.values.reduce(0, :+)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
## Moved to rankable.rb as mixins
|
|
34
|
+
# def thumbs_up
|
|
35
|
+
# @rank += 1
|
|
36
|
+
# end
|
|
37
|
+
# def thumbs_down
|
|
38
|
+
# @rank -= 1
|
|
39
|
+
# end
|
|
40
|
+
# def normalized_rank
|
|
41
|
+
# @rank / 10
|
|
42
|
+
# end
|
|
43
|
+
# def <=>(other_movie)
|
|
44
|
+
# other_movie.rank <=> @rank
|
|
45
|
+
# end
|
|
46
|
+
# def status
|
|
47
|
+
# # if hit?
|
|
48
|
+
# # 'Hit'
|
|
49
|
+
# # else
|
|
50
|
+
# # 'Flop'
|
|
51
|
+
# # end
|
|
52
|
+
# hit? ? 'Hit' : 'Flop'
|
|
53
|
+
# end
|
|
54
|
+
# def hit?
|
|
55
|
+
# @rank >= 10
|
|
56
|
+
# end
|
|
57
|
+
|
|
58
|
+
def to_s
|
|
59
|
+
"#{@title} has a rank of #{@rank} (#{status})"
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def to_csv
|
|
63
|
+
"#{@title},#{@rank}"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def normalized_rank_message
|
|
67
|
+
"#{@title} has a normalizer rank of #{normalized_rank}"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def each_snack
|
|
73
|
+
@snack_carbs.each do |name, carbs|
|
|
74
|
+
snack = Snack.new(name, carbs)
|
|
75
|
+
yield snack
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# Example code how to use Movie class
|
|
85
|
+
# Will run only by running this file and not inheriting it
|
|
86
|
+
|
|
87
|
+
if __FILE__ == $0
|
|
88
|
+
movie1 = Movie.new('goonies',10)
|
|
89
|
+
movie2 = Movie.new('up', 5)
|
|
90
|
+
movie3 = Movie.new('scarface')
|
|
91
|
+
|
|
92
|
+
movies = [movie1, movie2, movie3]
|
|
93
|
+
puts movies
|
|
94
|
+
movies.each do |m|
|
|
95
|
+
m.thumbs_up
|
|
96
|
+
puts m
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'flicks/movie'
|
|
2
|
+
|
|
3
|
+
# Movie3D inherit from Movie
|
|
4
|
+
class Movie3D < Movie
|
|
5
|
+
|
|
6
|
+
def initialize(title, rank, wow_factor)
|
|
7
|
+
super(title, rank) # so that not override initialize method in the superclasse
|
|
8
|
+
@wow_factor = wow_factor
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# def methor propre to Movie3D class
|
|
12
|
+
def show_effect
|
|
13
|
+
puts "Wow! " * @wow_factor
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Override thumbs_up method of Movie class
|
|
17
|
+
def thumbs_up
|
|
18
|
+
# access to rank : a subclasse has access to its superclass instance variables
|
|
19
|
+
# @rank += (1 * @wow_factor)
|
|
20
|
+
# We can instad call thumbs_up multiple times
|
|
21
|
+
@wow_factor.times { super }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# movie3d = Movie3D.new('glee', 5, 20)
|
|
26
|
+
# puts movie3d.title
|
|
27
|
+
# puts movie3d.rank
|
|
28
|
+
|
|
29
|
+
# movie3d.thumbs_up
|
|
30
|
+
# puts movie3d.rank
|
|
31
|
+
# puts movie3d
|
|
32
|
+
|
|
33
|
+
# movie3d.show_effect
|
|
34
|
+
|
|
35
|
+
# Check hiarchy using irb
|
|
36
|
+
|
|
37
|
+
# load 'movie3d.rb'
|
|
38
|
+
# => true
|
|
39
|
+
# Movie3D.superclass
|
|
40
|
+
# => Movie
|
|
41
|
+
# Movie3D.ancestors
|
|
42
|
+
# => [Movie3D, Movie, Object, Kernel, BasicObject]
|
|
43
|
+
|
|
44
|
+
# When calling a method : ruby try to find that method in the Classe, if not in the sperclass, ... and so on
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
module MovieSystem
|
|
2
|
+
|
|
3
|
+
VERSION = 1.0
|
|
4
|
+
|
|
5
|
+
def self.info
|
|
6
|
+
puts "Movie system version #{VERSION}"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class Player
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module GameSystem
|
|
14
|
+
|
|
15
|
+
VERSION = 2.0
|
|
16
|
+
|
|
17
|
+
def self.info
|
|
18
|
+
puts "Game system version #{VERSION}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Player
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
puts MovieSystem::VERSION
|
|
27
|
+
puts MovieSystem.info
|
|
28
|
+
puts MovieSystem::Player.new # We can use the same name for class Player using MovieSystem module and GameSystem module
|
|
29
|
+
|
|
30
|
+
puts GameSystem::VERSION
|
|
31
|
+
puts GameSystem.info
|
|
32
|
+
puts GameSystem::Player.new
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require 'flicks/movie'
|
|
2
|
+
require 'flicks/waldorf_and_statler'
|
|
3
|
+
require 'flicks/snack_bar'
|
|
4
|
+
|
|
5
|
+
module Flicks
|
|
6
|
+
class Playlist
|
|
7
|
+
def initialize(name)
|
|
8
|
+
@name = name
|
|
9
|
+
@movies = []
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def load(from_file)
|
|
13
|
+
File.readlines(from_file).each do |line|
|
|
14
|
+
# title, rank = line.split(',')
|
|
15
|
+
# movie = Movie.new(title, rank.to_i)
|
|
16
|
+
# add_movie(movie)
|
|
17
|
+
add_movie(Movie.from_csv(line))
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def save(to_file='movie_rankings.csv')
|
|
23
|
+
File.open(to_file, 'w') do |file|
|
|
24
|
+
@movies.sort.each do |movie|
|
|
25
|
+
file.puts movie.to_csv
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_movie(movie)
|
|
31
|
+
@movies << movie
|
|
32
|
+
end
|
|
33
|
+
def roll_die
|
|
34
|
+
rand(1..6)
|
|
35
|
+
end
|
|
36
|
+
def play(viewings)
|
|
37
|
+
puts "#{@name}'s playlist:"
|
|
38
|
+
# puts @movies
|
|
39
|
+
puts @movies.sort
|
|
40
|
+
|
|
41
|
+
snacks = SnackBar::SNACK
|
|
42
|
+
puts "\nThere are #{snacks.size} snacks available in the snack bar."
|
|
43
|
+
|
|
44
|
+
snacks.each do |snack|
|
|
45
|
+
puts "#{snack.name} has #{snack.carbs} carbs."
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# viewings.times do |count|
|
|
49
|
+
1.upto(viewings) do |count|
|
|
50
|
+
puts "\nViewing #{count}:"
|
|
51
|
+
@movies.each do |movie|
|
|
52
|
+
WaldorAndStatler.review(movie)
|
|
53
|
+
snack = SnackBar.random
|
|
54
|
+
movie.ate_snack(snack)
|
|
55
|
+
#puts "#{movie.title} led to #{snack.carbs} #{snack.name} carbs being consumed."
|
|
56
|
+
puts movie
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def total_carbs_consumed
|
|
62
|
+
@movies.reduce(0) do |sum, movie|
|
|
63
|
+
sum + movie.carbs_consumed
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def print_stats
|
|
68
|
+
# The name of the playlist here
|
|
69
|
+
puts "\n#{@name}'s Stats:"
|
|
70
|
+
|
|
71
|
+
puts "#{total_carbs_consumed} total carbs consumed"
|
|
72
|
+
@movies.sort.each do |movie|
|
|
73
|
+
puts "\n#{movie.title}'s snack totals:"
|
|
74
|
+
|
|
75
|
+
movie.each_snack do |snack|
|
|
76
|
+
puts "#{snack.carbs} total #{snack.name} carbs"
|
|
77
|
+
end
|
|
78
|
+
puts "#{movie.carbs_consumed} grand total carbs"
|
|
79
|
+
end
|
|
80
|
+
hits, flops = @movies.partition { |movie| movie.hit? }
|
|
81
|
+
|
|
82
|
+
puts "\nHits:"
|
|
83
|
+
# puts hits
|
|
84
|
+
puts hits.sort
|
|
85
|
+
puts "\nFlops:"
|
|
86
|
+
# puts flops
|
|
87
|
+
puts flops.sort
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Rankable
|
|
2
|
+
|
|
3
|
+
# with @rank instance variable this will work, but a general rule is that a module depends on an instance method/attribute
|
|
4
|
+
def thumbs_up
|
|
5
|
+
#@rank += 1
|
|
6
|
+
self.rank += 1 # here we re changing the rank, not only using the reader attribute, we need to change to accessor attribute
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def thumbs_down
|
|
10
|
+
#@rank -= 1
|
|
11
|
+
self.rank -= 1
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def normalized_rank
|
|
15
|
+
# @rank / 10
|
|
16
|
+
self.rank / 10
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def <=>(other)
|
|
20
|
+
# other.rank <=> @rank
|
|
21
|
+
other.rank <=> self.rank
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def status
|
|
25
|
+
hit? ? 'Hit' : 'Flop'
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def hit?
|
|
29
|
+
# @rank >= 10
|
|
30
|
+
self.rank >= 10
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# class Snack
|
|
2
|
+
# attr_reader :name, :carbs
|
|
3
|
+
|
|
4
|
+
# def initialize(name, carbs)
|
|
5
|
+
# @name = name
|
|
6
|
+
# @carbs = carbs
|
|
7
|
+
# end
|
|
8
|
+
# end
|
|
9
|
+
|
|
10
|
+
# Struct initiate a class with its accessors,
|
|
11
|
+
Snack = Struct.new(:name, :carbs)
|
|
12
|
+
|
|
13
|
+
# the objects within this class can be modified
|
|
14
|
+
# tasty_snack = Snack.new(:nachos, 40)
|
|
15
|
+
# tasty_snack.name = :totopos
|
|
16
|
+
# tasty_snack.carbs = 30
|
|
17
|
+
|
|
18
|
+
# popcorn = Snack.new('popcorn', 20)
|
|
19
|
+
# puts popcorn.name
|
|
20
|
+
# puts popcorn.carbs
|
|
21
|
+
|
|
22
|
+
# candy = Snack.new('candy', 15)
|
|
23
|
+
# puts candy.name
|
|
24
|
+
# puts candy.carbs
|
|
25
|
+
|
|
26
|
+
module SnackBar
|
|
27
|
+
|
|
28
|
+
# Name in upcase means it is a constant in ruby
|
|
29
|
+
SNACK = [
|
|
30
|
+
Snack.new(:popcorn, 20),
|
|
31
|
+
Snack.new(:candy, 15),
|
|
32
|
+
Snack.new(:nachos, 40),
|
|
33
|
+
Snack.new(:pretzel, 10),
|
|
34
|
+
Snack.new(:soda, 5)
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
def self.random
|
|
38
|
+
SNACK.sample # sample in ruby applied to arrays will return a random object
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
if __FILE__ == $0
|
|
44
|
+
# to get access the constant inside the module we use ::
|
|
45
|
+
puts SnackBar::SNACK
|
|
46
|
+
snack = SnackBar.random
|
|
47
|
+
puts "Enjoy your #{snack.name} (#{snack.carbs} carbs)"
|
|
48
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
names = %w(Goonies Ghosbusters Glodfinger Godfather)
|
|
2
|
+
# sort the array by alphabetical order
|
|
3
|
+
# puts names.sort
|
|
4
|
+
|
|
5
|
+
# sort by something else, like word length
|
|
6
|
+
# puts names.sort_by {|w| w.length}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
require_relative 'movie'
|
|
10
|
+
|
|
11
|
+
movie1 = Movie.new('Goonies',10)
|
|
12
|
+
movie2 = Movie.new('Ghosbusters',3)
|
|
13
|
+
movie3 = Movie.new('Glodfinger',7)
|
|
14
|
+
|
|
15
|
+
movies = [movie1, movie2, movie3]
|
|
16
|
+
|
|
17
|
+
# sort by rank
|
|
18
|
+
# puts movies.sort_by { |movie| movie.rank }
|
|
19
|
+
# sort by rank in reverse order
|
|
20
|
+
#puts movies.sort_by { |movie| movie.rank }.reverse
|
|
21
|
+
|
|
22
|
+
# ruby spaceship operartor : general comparison operator
|
|
23
|
+
#puts movie1.rank <=> movie2.rank # returns 1
|
|
24
|
+
#puts movie2.rank <=> movie1.rank # returns -1
|
|
25
|
+
|
|
26
|
+
# add method sort to Movie class : because sort is known for ruby to just compare strigs, numbers ...
|
|
27
|
+
|
|
28
|
+
puts movies.sort
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
:green # this is a symbol
|
|
2
|
+
:green.class # returns Symbol
|
|
3
|
+
|
|
4
|
+
'green' # a string
|
|
5
|
+
'green'.class # returns String
|
|
6
|
+
|
|
7
|
+
:geen.object_id # always the same id for green
|
|
8
|
+
'green'.object_id # different obj id for each string
|
|
9
|
+
|
|
10
|
+
'green' == :green #Symbols are not strings
|
|
11
|
+
|
|
12
|
+
# to convert a symbol to a string
|
|
13
|
+
:green.to_s
|
|
14
|
+
|
|
15
|
+
# to convert a string to a symbol
|
|
16
|
+
'green'.to_sym
|
|
17
|
+
|
|
18
|
+
# all the methods a symbol has
|
|
19
|
+
Symbol.instance_methods
|
|
20
|
+
|
|
21
|
+
# Strings Vs. Symbols
|
|
22
|
+
# Symbols : to name or identify something in the code. Eg. attr_reader :name
|
|
23
|
+
# Strings : do text processing. Eg. puts "#{name.capitalize} has a health of 90"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module WaldorAndStatler
|
|
2
|
+
# def self.review #Module method begin with self.
|
|
3
|
+
# puts 'Bravo!'
|
|
4
|
+
# end
|
|
5
|
+
|
|
6
|
+
def self.roll_die
|
|
7
|
+
rand(1..6)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def self.review(movie)
|
|
11
|
+
number_rolled = roll_die
|
|
12
|
+
case number_rolled
|
|
13
|
+
when 1..2
|
|
14
|
+
movie.thumbs_down
|
|
15
|
+
puts "#{movie.title} got a thumbs down."
|
|
16
|
+
when 3..4
|
|
17
|
+
puts "#{movie.title} was skipped."
|
|
18
|
+
else
|
|
19
|
+
movie.thumbs_up
|
|
20
|
+
puts "#{movie.title} got a thumbs up!"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# WaldorAndStatler.new #error because a module doesn't instantiate -> cant't create objects
|
|
28
|
+
# WaldorAndStatler.review
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: my-flicks
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Aymen Chetoui
|
|
@@ -36,10 +36,26 @@ files:
|
|
|
36
36
|
- bin/movies.csv
|
|
37
37
|
- bin/movie_rankings.csv
|
|
38
38
|
- bin/superheros_movies.csv
|
|
39
|
-
-
|
|
40
|
-
-
|
|
39
|
+
- lib/flicks/blocks.rb
|
|
40
|
+
- lib/flicks/conditionals.rb
|
|
41
|
+
- lib/flicks/file.rb
|
|
42
|
+
- lib/flicks/fliks_bis.rb
|
|
43
|
+
- lib/flicks/hash.rb
|
|
44
|
+
- lib/flicks/iterators.rb
|
|
45
|
+
- lib/flicks/mixins.rb
|
|
46
|
+
- lib/flicks/movie.rb
|
|
47
|
+
- lib/flicks/movie3d.rb
|
|
48
|
+
- lib/flicks/namespaces.rb
|
|
49
|
+
- lib/flicks/playlist.rb
|
|
50
|
+
- lib/flicks/rankable.rb
|
|
51
|
+
- lib/flicks/snack_bar.rb
|
|
52
|
+
- lib/flicks/sorting.rb
|
|
53
|
+
- lib/flicks/symbols.rb
|
|
54
|
+
- lib/flicks/waldorf_and_statler.rb
|
|
41
55
|
- spec/flicks/movie_spec.rb
|
|
42
56
|
- spec/flicks/playlist_spec.rb
|
|
57
|
+
- LICENCE
|
|
58
|
+
- README
|
|
43
59
|
homepage: ''
|
|
44
60
|
licenses: []
|
|
45
61
|
metadata: {}
|