Class: Article
- Inherits:
-
Object
- Object
- Article
- Defined in:
- lib/article.rb
Constant Summary collapse
- @@articles =
[]
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#story ⇒ Object
Returns the value of attribute story.
-
#title ⇒ Object
Returns the value of attribute title.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .all ⇒ Object
- .create_article_from_hash(hash) ⇒ Object
- .create_articles_from_array(array) ⇒ Object
-
.p_wrap(s, width = 78) ⇒ Object
following method was taken from www.safaribooksonline.com/library/view/ruby-cookbook/0596523696/ch01s15.html.
- .word_wrap(text) ⇒ Object
Instance Method Summary collapse
-
#initialize(title = nil, url = nil, author = nil, story = nil) ⇒ Article
constructor
A new instance of Article.
Constructor Details
#initialize(title = nil, url = nil, author = nil, story = nil) ⇒ Article
Returns a new instance of Article.
8 9 10 11 12 13 |
# File 'lib/article.rb', line 8 def initialize(title = nil, url = nil, = nil, story = nil) @title = title @author = @url = url @story = story end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
3 4 5 |
# File 'lib/article.rb', line 3 def @author end |
#story ⇒ Object
Returns the value of attribute story.
3 4 5 |
# File 'lib/article.rb', line 3 def story @story end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/article.rb', line 3 def title @title end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/article.rb', line 3 def url @url end |
Class Method Details
.all ⇒ Object
27 28 29 |
# File 'lib/article.rb', line 27 def self.all @@articles end |
.create_article_from_hash(hash) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/article.rb', line 32 def self.create_article_from_hash(hash) a = Article.new a.title = hash[:title] a.url = hash[:url] a. = hash[:author] string = hash[:story] a.story = self.word_wrap(hash[:story]) a end |
.create_articles_from_array(array) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/article.rb', line 16 def self.create_articles_from_array(array) array.each do |article| title = article[:title] url = article[:url] a = Article.new(title, url) @@articles << a end @@articles end |
.p_wrap(s, width = 78) ⇒ Object
following method was taken from www.safaribooksonline.com/library/view/ruby-cookbook/0596523696/ch01s15.html
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/article.rb', line 59 def self.p_wrap(s, width=78) lines = [] line = "" s.split(/\s+/).each do |word| if line.size + word.size >= width lines << line line = word elsif line.empty? line = word else line << " " << word end end lines << line if line return lines.join "\n" end |
.word_wrap(text) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/article.rb', line 42 def self.word_wrap(text) paragraphs = [] paragraphs = text.split(/\n/) finshed_text = "" paragraphs.each do |p| final_p = self.p_wrap(p, 78) finshed_text << "\n" + final_p end finshed_text end |