Class: String

Inherits:
Object show all
Defined in:
lib/fOOrth/monkey_patch/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

#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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fOOrth/library/introspection/string.rb', line 7

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 {|a,b| a.foorth_name <=> b.foorth_name}
    .each do |klass|
      spec, info = klass.map_foorth_shared_info(symbol, :shallow)
      found |= spec && (results << ["", ""]).concat(info).concat(spec.get_info)

      spec, info = klass.map_foorth_exclusive_info(symbol, :shallow)
      found |= spec && (results << ["", ""]).concat(info).concat(spec.get_info)
    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

#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

#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