Class: LongTweet::IdealSplitter

Inherits:
Splitter
  • Object
show all
Defined in:
lib/long_tweet/ideal_splitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Splitter

#set_splitter, #tweet_count

Constructor Details

#initialize(text) ⇒ IdealSplitter

Returns a new instance of IdealSplitter.



4
5
6
# File 'lib/long_tweet/ideal_splitter.rb', line 4

def initialize text
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



3
4
5
# File 'lib/long_tweet/ideal_splitter.rb', line 3

def text
  @text
end

Instance Method Details

#splitObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/long_tweet/ideal_splitter.rb', line 8

def split
  a = []
  ary = text.split
  tweet = ''
  while ary.length > 0
    while (tweet.length + ary.first.length) < 140
      tweet << "#{ary.shift} "
      break if ary.empty?
    end
    a << Tweet.new(tweet.strip)
    tweet = ''
  end
  a
end