Class: Character
- Inherits:
-
Object
- Object
- Character
- Defined in:
- lib/character.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
- .list_all_characters ⇒ Object
- .list_all_quotes_by_character ⇒ Object
- .list_all_quotes_for_character(name) ⇒ Object
- .random_quote_for_character(name) ⇒ Object
Instance Method Summary collapse
- #add_quote(quote) ⇒ Object
-
#initialize(name) ⇒ Character
constructor
A new instance of Character.
- #quotes ⇒ Object
Constructor Details
#initialize(name) ⇒ Character
Returns a new instance of Character.
7 8 9 10 11 |
# File 'lib/character.rb', line 7 def initialize(name) @name = name @quotes = [] @@all << self end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/character.rb', line 3 def name @name end |
Class Method Details
.all ⇒ Object
26 27 28 |
# File 'lib/character.rb', line 26 def self.all @@all end |
.list_all_characters ⇒ Object
30 31 32 33 34 |
# File 'lib/character.rb', line 30 def self.list_all_characters @@all.each do |character| puts character.name end end |
.list_all_quotes_by_character ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/character.rb', line 36 def self.list_all_quotes_by_character @@all.each do |character| i = 0 puts "Quotes for #{character.name}: " character.quotes.each do |quote| i += 1 puts "#{i}. #{quote.content}" end end end |
.list_all_quotes_for_character(name) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/character.rb', line 47 def self.list_all_quotes_for_character(name) @@all.each do |character| i = 0 if character.name == name puts "Quotes for #{character.name}: " character.quotes.each do |quote| i += 1 puts "#{i}. #{quote.content}" end end end end |
.random_quote_for_character(name) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/character.rb', line 60 def self.random_quote_for_character(name) @@all.each do |character| if character.name == name n = character.quotes.size - 1 r = rand(0..n) puts "#{character.quotes[r].content}" end end end |
Instance Method Details
#add_quote(quote) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/character.rb', line 13 def add_quote(quote) if !@quotes.include?(quote) @quotes << quote end if quote.character == nil quote.character = self end end |
#quotes ⇒ Object
22 23 24 |
# File 'lib/character.rb', line 22 def quotes @quotes end |