Class: Wc

Inherits:
Object
  • Object
show all
Defined in:
lib/wc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#filenameObject (readonly)

Returns the value of attribute filename.



2
3
4
# File 'lib/wc.rb', line 2

def filename
  @filename
end

#hide_listObject

Returns the value of attribute hide_list.



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

def hide_list
  @hide_list
end

#occurrencesObject (readonly)

Returns the value of attribute occurrences.



2
3
4
# File 'lib/wc.rb', line 2

def occurrences
  @occurrences
end

#wordsObject (readonly)

Returns the value of attribute words.



2
3
4
# File 'lib/wc.rb', line 2

def words
  @words
end

Instance Method Details

#to_cloudObject



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_jsonObject



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_textObject



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