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

#==(other) ⇒ Object



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

def ==(other)
  other.is_a?(AnyToString)
end

#call(vm) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/syntax_tree/yarv/instructions.rb', line 174

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



170
171
172
# File 'lib/syntax_tree/yarv/instructions.rb', line 170

def canonical
  self
end

#deconstruct_keys(_keys) ⇒ Object



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

def deconstruct_keys(_keys)
  {}
end

#disasm(fmt) ⇒ Object



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

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

#lengthObject



158
159
160
# File 'lib/syntax_tree/yarv/instructions.rb', line 158

def length
  1
end

#popsObject



162
163
164
# File 'lib/syntax_tree/yarv/instructions.rb', line 162

def pops
  2
end

#pushesObject



166
167
168
# File 'lib/syntax_tree/yarv/instructions.rb', line 166

def pushes
  1
end

#to_a(_iseq) ⇒ Object



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

def to_a(_iseq)
  [:anytostring]
end