Class: TextChunk

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

Overview

Biblioteca disponibilizada pelo professor para a impletemetação

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ TextChunk

Returns a new instance of TextChunk.



4
5
6
7
8
9
10
11
# File 'lib/text_chunk.rb', line 4

def initialize(n)
    case n
    when String
        @stringVal = n
    when Numeric
        @stringVal = numericToString(n)
    end
end

Class Method Details

.block_size(n) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/text_chunk.rb', line 43

def self.block_size(n)
    block_size = 0
    temp = n - 1
    while temp > 1
        temp = temp / 2
        block_size += 1
    end

    return block_size / 8
end

Instance Method Details

#numericToString(n) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/text_chunk.rb', line 13

def numericToString(n)
    strVal = ""
    if n == 0
        strVal = "0"
    else
        while n > 0
            ans = n.divmod(256)
            charNum = ans[1]
            strVal += charNum.chr
            n = ans[0]
        end
    end
    return strVal
end

#to_iObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/text_chunk.rb', line 32

def to_i
    result = 0

    @stringVal.reverse.split('').each{ | c |
        result = result * 256
        result += c.ord
    }

    return result
end

#to_sObject



28
29
30
# File 'lib/text_chunk.rb', line 28

def to_s
    return @stringVal
end