Class: String

Inherits:
Object show all
Defined in:
lib/fOOrth/monkey_patch/string.rb,
lib/fOOrth/library/formatting/string.rb,
lib/fOOrth/library/introspection/string.rb

Overview

  • library/introspection/string.rb - String support for introspection.

Direct Known Subclasses

StringBuffer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_foorth_instance(vm) ⇒ Object

Create an instance of a String.
Parameters:

  • vm - The current fOOrth virtual machine.



69
70
71
72
# File 'lib/fOOrth/monkey_patch/string.rb', line 69

def self.create_foorth_instance(vm)
  (obj = "".freeze).foorth_init(vm)
  obj
end

Instance Method Details

#do_format_description(input, max_width) ⇒ Object

Do the formatting legwork.



12
13
14
15
16
17
18
19
20
21
# File 'lib/fOOrth/library/formatting/string.rb', line 12

def do_format_description(input, max_width)
  result, build = [], ""

  loop do
    build = build.split_if_over(input.next, max_width, result)
                 .split_if_huge(max_width, result)
  end

  result << build
end

#foorth_coerce(arg) ⇒ Object

Coerce the argument to match my type.
Endemic Code Smells

  • :reek:FeatureEnvy – false positive



15
16
17
18
19
# File 'lib/fOOrth/monkey_patch/string.rb', line 15

def foorth_coerce(arg)
  arg.to_s.freeze
rescue
  error "F40: Cannot coerce a #{arg.foorth_name} to an String instance"
end

#foorth_embedObject

Convert this String to a form suitable for embedding in a source string.
Returns

  • An embeddable form of this string as a string.



8
9
10
# File 'lib/fOOrth/monkey_patch/string.rb', line 8

def foorth_embed
  self.inspect
end

#foorth_method_scanObject

Scan all classes for information about a method.
Endemic Code Smells

  • :reek:DuplicateMethodCall :reek:FeatureEnvy :reek:TooManyStatements



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fOOrth/library/introspection/string.rb', line 9

def foorth_method_scan
  symbol, results = XfOOrth::SymbolMap.map_info(self)
  found = false

  symbol && $FOORTH_GLOBALS.values
    .select {|entry| entry.has_tag?(:class)}
    .collect {|spec| spec.new_class}
    .sort {|first, second| first.foorth_name <=> second.foorth_name}
    .each do |klass|
      shared_spec, shared_info = klass.map_foorth_shared_info(symbol, :shallow)

      if shared_spec
        results
          .concat([["", ""]])
          .concat(shared_info)
          .concat(shared_spec.get_info)
      end

      excl_spec, excl_info = klass.map_foorth_exclusive_info(symbol, :shallow)

      if excl_spec
        results
          .concat([["", ""]])
          .concat(excl_info)
          .concat(excl_spec.get_info)
      end

      found ||= (shared_spec || excl_spec)
    end

  results << ["Scope", "not found in any class."] if symbol && !found

  results
end

#foorth_string_freezeObject

Freeze only pure strings



62
63
64
# File 'lib/fOOrth/monkey_patch/string.rb', line 62

def foorth_string_freeze
  self.freeze
end

#format_description(max_width) ⇒ Object

Create a bullet point description from this string.



7
8
9
# File 'lib/fOOrth/library/formatting/string.rb', line 7

def format_description(max_width)
  do_format_description(split(' ').each, max_width)
end

#full_clone(_arg = nil) ⇒ Object

A special patch for full_clone



57
58
59
# File 'lib/fOOrth/monkey_patch/string.rb', line 57

def full_clone(_arg=nil)
  self.freeze
end

#safe_cloneObject

A special patch for safe_clone



52
53
54
# File 'lib/fOOrth/monkey_patch/string.rb', line 52

def safe_clone
  self.freeze
end

#split_if_huge(max_width, buffer) ⇒ Object

Split up a overlong blob of text.



38
39
40
41
42
43
44
# File 'lib/fOOrth/library/formatting/string.rb', line 38

def split_if_huge(max_width, buffer)
  while length >= max_width
    buffer << slice!(0, max_width)
  end

  self
end

#split_if_over(word, max_width, buffer) ⇒ Object

Split if adding a word goes over a little.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fOOrth/library/formatting/string.rb', line 24

def split_if_over(word, max_width, buffer)
  word.prepend(" ") unless self.empty?
  word_len = word.length

  if (length + word_len) >= max_width && word_len < max_width
    buffer << self
    word.lstrip
  else
    self + word
  end

end

#to_foorth_cObject

Convert this string to a single character string.



22
23
24
# File 'lib/fOOrth/monkey_patch/string.rb', line 22

def to_foorth_c
  self[0]
end

#to_foorth_nObject

Convert this string to a numeric. Return a number or nil on fail.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fOOrth/monkey_patch/string.rb', line 27

def to_foorth_n
  if /\di$/ =~ self      #Check for a trailing '<digit>i'.
    #Check for the internal '+' or '-'sign.
    if /(?<=\d)[+-]/ =~ self
      Complex(($PREMATCH).to_foorth_n, ($MATCH + $POSTMATCH).chop.to_foorth_n)
    else
      Complex(0, self.chop.to_foorth_n)
    end
  elsif /\d\/\d/ =~ self #Check for an embedded '<digit>/<digit>'.
    Rational(self)
  elsif /\d\.\d/ =~ self #Check for an embedded '<digit>.<digit>'.
    Float(self)
  else                   #For the rest, assume an integer.
    Integer(self)
  end
rescue
  nil
end

#to_foorth_rObject

Convert this string to a rational. Return a number or nil on fail.



47
48
49
# File 'lib/fOOrth/monkey_patch/string.rb', line 47

def to_foorth_r
  self.to_foorth_n.to_foorth_r
end