How can I track line creation in Ruby?

In this answer to a separate question , I said that if you do

name = "Rohit " "Sharma"

you do not create two objects Stringwith contents "Rohit "and "Sharma"that combine to create a new String object with contents "Rohit Sharma", but you only create one String object to start with.

But this is only a book telling me this, not a manual check.

How could you write line creation?

I tried to use

set_trace_func proc { |event, file, line, id, binding, classname|
  printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}

string = "Insert content here"

But received only

c-return -:3  set_trace_func   Kernel
    line -:5

And "Programming Ruby 1.9" (The Pickaxe) says the change Class#newwill not work for strings, as they are built without use new.

+3
source share
2

, , :

( Pickaxe " : Ruby VM" ) YARV - , - ruby:

irb(main):003:0> uncompiled_code = 'name = "Rohit " "Sharma"'
=> "name = \"Rohit \" \"Sharma\""
irb(main):004:0> code = RubyVM::InstructionSequence.compile(uncompiled_code)
=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
irb(main):005:0> puts code.disassemble
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] name
0000 trace            1                                               (   1)
0002 putstring        "Rohit Sharma"
0004 dup
0005 setlocal         name
0007 leave
=> nil
0

, . .

: , , , .

: , , , , 42 Fixnum 42, 42 Fixnum 1: Ruby ISO , Ruby , Ruby .

+3

Source: https://habr.com/ru/post/1778865/


All Articles