Class: Basic101::BasicString
Instance Attribute Summary collapse
Instance Method Summary
collapse
comparison_op
Methods inherited from BasicObject
#eval, #to_float, #to_integer, #to_numeric, #type_name, type_name
Constructor Details
Returns a new instance of BasicString.
10
11
12
|
# File 'lib/basic101/basic_string.rb', line 10
def initialize(value)
@value = value.to_s
end
|
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
8
9
10
|
# File 'lib/basic101/basic_string.rb', line 8
def value
@value
end
|
Instance Method Details
#+(other) ⇒ Object
66
67
68
|
# File 'lib/basic101/basic_string.rb', line 66
def +(other)
BasicString.new(value + other.to_string.value)
end
|
#<=>(other) ⇒ Object
14
15
16
17
|
# File 'lib/basic101/basic_string.rb', line 14
def <=>(other)
return nil unless other.is_a?(self.class)
value <=> other.value
end
|
#left(count) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/basic101/basic_string.rb', line 31
def left(count)
count = count.to_i
if count < 0
raise InvalidArgumentError
end
BasicString.new(@value[0...count])
end
|
#length ⇒ Object
48
49
50
|
# File 'lib/basic101/basic_string.rb', line 48
def length
BasicInteger.new(@value.size)
end
|
#mid(start, count = nil) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/basic101/basic_string.rb', line 52
def mid(start, count = nil)
start = start.to_i
count = count && count.to_i
raise InvalidArgumentError if start < 1
raise InvalidArgumentError if count && count < 1
start -= 1
substring = if count
@value[start, count]
else
@value[start..-1]
end
self.class.new(substring)
end
|
#print_new_line(output) ⇒ Object
23
24
25
|
# File 'lib/basic101/basic_string.rb', line 23
def print_new_line(output)
output.print "\n"
end
|
#print_string(output) ⇒ Object
19
20
21
|
# File 'lib/basic101/basic_string.rb', line 19
def print_string(output)
output.print @value
end
|
#right(count) ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/basic101/basic_string.rb', line 39
def right(count)
count = count.to_i
if count < 0
raise InvalidArgumentError
end
substring = @value[/.{0,#{count}}$/]
BasicString.new(substring)
end
|
#str ⇒ Object
79
80
81
|
# File 'lib/basic101/basic_string.rb', line 79
def str
self
end
|
#to_string ⇒ Object
27
28
29
|
# File 'lib/basic101/basic_string.rb', line 27
def to_string
self
end
|