class String

Extensions to the String class required by the fOOrth language system.

Public Class Methods

create_foorth_instance(vm) click to toggle source

Create an instance of a String.
Parameters:

  • vm - The current fOOrth virtual machine.

# File lib/fOOrth/monkey_patch/string.rb, line 69
def self.create_foorth_instance(vm)
  (obj = "".freeze).foorth_init(vm)
  obj
end

Public Instance Methods

do_format_description(input, max_width) click to toggle source

Do the formatting legwork.

# 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) click to toggle source

Coerce the argument to match my type.
Endemic Code Smells

  • :reek:FeatureEnvy – false positive

# 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_embed() click to toggle source

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

  • An embeddable form of this string as a string.

# File lib/fOOrth/monkey_patch/string.rb, line 8
def foorth_embed
  self.inspect
end
foorth_method_scan() click to toggle source

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

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

# 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_freeze() click to toggle source

Freeze only pure strings

# File lib/fOOrth/monkey_patch/string.rb, line 62
def foorth_string_freeze
  self.freeze
end
format_description(max_width) click to toggle source

Create a bullet point description from this string.

# 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) click to toggle source

A special patch for full_clone

# File lib/fOOrth/monkey_patch/string.rb, line 57
def full_clone(_arg=nil)
  self.freeze
end
safe_clone() click to toggle source

A special patch for safe_clone

# File lib/fOOrth/monkey_patch/string.rb, line 52
def safe_clone
  self.freeze
end
split_if_huge(max_width, buffer) click to toggle source

Split up a overlong blob of text.

# 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) click to toggle source

Split if adding a word goes over a little.

# 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_c() click to toggle source

Convert this string to a single character string.

# File lib/fOOrth/monkey_patch/string.rb, line 22
def to_foorth_c
  self[0]
end
to_foorth_n() click to toggle source

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

# 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_r() click to toggle source

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

# File lib/fOOrth/monkey_patch/string.rb, line 47
def to_foorth_r
  self.to_foorth_n.to_foorth_r
end