Class: Wc
- Inherits:
-
Object
- Object
- Wc
- Defined in:
- lib/wc.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#hide_list ⇒ Object
Returns the value of attribute hide_list.
-
#occurrences ⇒ Object
readonly
Returns the value of attribute occurrences.
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
-
#initialize(filename, words, hide_list) ⇒ Wc
constructor
A new instance of Wc.
- #to_cloud ⇒ Object
- #to_json ⇒ Object
- #to_text ⇒ Object
Constructor Details
#initialize(filename, words, hide_list) ⇒ Wc
Returns a new instance of Wc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/wc.rb', line 5 def initialize(filename, words, hide_list) puts filename if ! filename.nil? @filename = filename @hide_list = hide_list @occurrences = read else @filename = STDIN @hide_list = hide_list @occurrences = feed end @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) } @words = words end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
2 3 4 |
# File 'lib/wc.rb', line 2 def filename @filename end |
#hide_list ⇒ Object
Returns the value of attribute hide_list.
3 4 5 |
# File 'lib/wc.rb', line 3 def hide_list @hide_list end |
#occurrences ⇒ Object (readonly)
Returns the value of attribute occurrences.
2 3 4 |
# File 'lib/wc.rb', line 2 def occurrences @occurrences end |
#words ⇒ Object (readonly)
Returns the value of attribute words.
2 3 4 |
# File 'lib/wc.rb', line 2 def words @words end |
Instance Method Details
#to_cloud ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/wc.rb', line 43 def to_cloud if @words == -1 cloud_items = @sorted else cloud_item = @sorted[0..@words-1] end ret = "<dl>" i=1 cloud_item.each { |elem| ret+="<dt id=\"a"+i.to_s+"\">" + elem[0] +"</dt>" i+=1 } ret += "</dl>" ret end |
#to_json ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/wc.rb', line 35 def to_json if @words == -1 return @sorted.to_json else return @sorted[0..@words-1].to_json end end |
#to_text ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/wc.rb', line 23 def to_text if @words == -1 @sorted.each { |elem| puts "\"#{elem[0]}\" has #{elem[1]} occurrences" } else @sorted[0..@words-1].each { |elem| puts "\"#{elem[0]}\" has #{elem[1]} occurrences" } end end |