Class: SyntaxTree::YARV::AnyToString

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

anytostring ensures that the value on top of the stack is a string.

It pops two values off the stack. If the first value is a string it pushes it back on the stack. If the first value is not a string, it uses Ruby’s built in string coercion to coerce the second value to a string and then pushes that back on the stack.

This is used in conjunction with objtostring as a fallback for when an object’s to_s method does not return a string.

### Usage

~~~ruby “#5” ~~~

Instance Method Summary collapse

Instance Method Details

#call(vm) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/syntax_tree/yarv/instructions.rb', line 158

def call(vm)
  original, value = vm.pop(2)

  if value.is_a?(String)
    vm.push(value)
  else
    vm.push("#<#{original.class.name}:0000>")
  end
end

#canonicalObject



154
155
156
# File 'lib/syntax_tree/yarv/instructions.rb', line 154

def canonical
  self
end

#disasm(fmt) ⇒ Object



134
135
136
# File 'lib/syntax_tree/yarv/instructions.rb', line 134

def disasm(fmt)
  fmt.instruction("anytostring")
end

#lengthObject



142
143
144
# File 'lib/syntax_tree/yarv/instructions.rb', line 142

def length
  1
end

#popsObject



146
147
148
# File 'lib/syntax_tree/yarv/instructions.rb', line 146

def pops
  2
end

#pushesObject



150
151
152
# File 'lib/syntax_tree/yarv/instructions.rb', line 150

def pushes
  1
end

#to_a(_iseq) ⇒ Object



138
139
140
# File 'lib/syntax_tree/yarv/instructions.rb', line 138

def to_a(_iseq)
  [:anytostring]
end