Skip to content

Instantly share code, notes, and snippets.

@patshaughnessy
Created July 30, 2012 10:42
Show Gist options
  • Save patshaughnessy/3206130 to your computer and use it in GitHub Desktop.
Save patshaughnessy/3206130 to your computer and use it in GitHub Desktop.
The "tostring" instruction below calls into this C code, from string.c. This checks if the expression/object is already a string, and does nothing if it is. If it's not a string, it calls "to_s" on the object.
VALUE
rb_obj_as_string(VALUE obj)
{
VALUE str;
if (RB_TYPE_P(obj, T_STRING)) {
return obj;
}
str = rb_funcall(obj, id_to_s, 0);
if (!RB_TYPE_P(str, T_STRING))
return rb_any_to_s(obj);
if (OBJ_TAINTED(obj)) OBJ_TAINT(str);
return str;
}
Ruby code:
a = 1
b = 2
puts "This is the sum: #{a+b}"
YARV instructions:
== disasm: <RubyVM::InstructionSequence:<main>@self1.rb>================
local table (size: 3, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 3] a [ 2] b
0000 trace 1 ( 3)
0002 putobject 1
0004 setdynamic a, 0
0007 trace 1 ( 4)
0009 putobject 2
0011 setdynamic b, 0
0014 trace 1 ( 5)
0016 putself
0017 putobject "This is the sum: "
0019 getdynamic a, 0
0022 getdynamic b, 0
0025 opt_plus <ic:2>
0027 tostring
0028 concatstrings 2
0030 send :puts, 1, nil, 8, <ic:1>
0036 leave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment