How das Ruby work

Hi together,

Today I was looking for some (almost) scientific sources (pdf, articles,
…) that give an insight what really happens behind the scenes and that
explains the technical details of Ruby; how does Ruby actually work??

Unfortunately I only found language references, how to code guides and
alike. Nothing that states what is really happening with my piece of
code when I call ‘ruby myfile.rb’

If you guys know some sources of such pdfs, books, articles, please let
me know.

Thanks
Stefan

Hi,

It’s an interpreter, so what happens is… you run your code with the
“ruby” command, which does the following:

  1. The interpreter goes through your code, instruction by instruction.
  2. As it goes through your code, it converts each instruction into
    machine language.
  3. Instruction by instruction, as it is converted to machine language,
    it is executed.

This is akin to a human language interpreter (rather than a human
language translator) - ie people whose job it is to convert
communication between two people speaking different languages.

A human language interpreter will translate each phrase as it is
spoken, whereas a human language translator isn’t under the “hammer”
so much so to speak, so the product that they produce will be akin to
a piece of software that is compiled - once finished, it is much
faster to run the code, and likewise once translated, it is much
faster to communicate the required communication.

Hope this helps.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #4 coming soon!
http://sensei.zenunit.com/

Hi Julian,

thanks. That helps a little.

Where does C come into play? I assume that’s the language the parser is
written in?
Are there papers that can be quoted? I need it for a paper in
University, thus quoting a forum is considered not appropriate.

Have a good day
STefan

Hi Stefan,

I have no idea if there area papers. I don’t imagine there would be,
and if there are, they’d probably be in Japanese.

Yes, C is the language the Interpreter is written in. Generally people
don’t write code in Assembler any more. It’s too machine-specific.
This information can all be found in Wikipedia, or by using google.
There will be countless papers on it.

Note that interpreted languages have been around for decades,
therefore finding information on how they work generally will apply
just fine to Ruby.

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #4 coming soon!
http://sensei.zenunit.com/

Paul Mueller wrote:

STefan

There have not been many “scholarly” articles written about Ruby (as
evidenced by quick searches on Google Scholar and acm.org) so you
will likely need to reference one of the books written on Ruby such as
“Programming Ruby”.

-Justin

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stefan Huettenrauch wrote:

Hi together,

Today I was looking for some (almost) scientific sources (pdf, articles,
…) that give an insight what really happens behind the scenes and that
explains the technical details of Ruby; how does Ruby actually work??

Hi Stefan,

the following may be a suitable paper:
http://portal.acm.org/citation.cfm?doid=1094855.1094912

It’s from 2005, not free, and it documents the upcoming version of Ruby,
currently distributed as a development release (1.9).

But I think it’s somewhat close to what you’re asking.

Cheers,
Antonio


http://antoniocangiano.com - Zen and the Art of Programming
http://stacktrace.it - Aperiodico di resistenza informatica
http://math-blog.com - Math Blog: Mathematics is wonderful!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgEJvgACgkQqCqsu0qUj9R6jACgwxueXbAVPdb71srpUw0/Qe8H
2KYAn3Y6M9bujiAX7OtEqe2cKHw5KIPW
=ntxu
-----END PGP SIGNATURE-----

From: “Stefan Huettenrauch” [email protected]

me know.
Charles Thornton has been moving ahead with a Japanese → English
translation of the Ruby Hackers Guide, which has many chapters focused
on the ruby internals. It is a work in progress, but the latest release
is
here:

http://www.hawthorne-press.com/Release_Integrated_RHG_Dec_24_2007.tgz

Also, you may find the instruction sequence disassembler in ruby 1.9
useful. For example:

irb(main):001:0> RUBY_VERSION
=> “1.9.0”
irb(main):002:0> is = VM::InstructionSequence.compile(‘foo.bar = 42’)
=> <ISeq:@>
irb(main):003:0> puts is.disasm
== disasm: <ISeq:@>=================================
0000 putnil (
1)
0001 putnil
0002 send :foo, 0, nil, 24,
0008 putobject 42
0010 setn 2
0012 send :bar=, 1, nil, 0,
0018 pop
0019 leave
=> nil

This allows one to compile ruby expressions to YARV bytecode, and
disassemble the bytecode.

I think the ‘send’ at offset 0002 may be particularly interesting. I am
not myself familiar with the 1.9 VM instructions yet, but it appears the
send primitive is used both for local variable lookup, as well as
sending
messages to objects. (Note that the same instruction sequence is
compiled regardless of whether any local variable named ‘foo’ actually
exists or not when the sequence is compiled… Thus it would seem
that even binding to local variables is completely dynamic in ruby.)

Rather than using the send primitive, there seem to be custom primitives
for global- and instance-variable lookup:

irb(main):008:0> is3 = VM::InstructionSequence.compile(‘$foo.bar = 42’)
=> <ISeq:@>
irb(main):009:0> puts is3.disasm
== disasm: <ISeq:@>=================================
0000 putnil (
1)
0001 getglobal $foo
0003 putobject 42
0005 setn 2
0007 send :bar=, 1, nil, 0,
0013 pop
0014 leave
=> nil

irb(main):010:0> is4 = VM::InstructionSequence.compile(‘@foo.bar = 42’)
=> <ISeq:@>
irb(main):011:0> puts is4.disasm
== disasm: <ISeq:@>=================================
0000 putnil (
1)
0001 getinstancevariable :@foo
0003 putobject 42
0005 setn 2
0007 send :bar=, 1, nil, 0,
0013 pop
0014 leave
=> nil

Anyway, fun stuff. :slight_smile:

Regards,

Bill

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stefan Huettenrauch wrote:
| Hi together,
|
| Today I was looking for some (almost) scientific sources (pdf, articles,
| …) that give an insight what really happens behind the scenes and that
| explains the technical details of Ruby; how does Ruby actually work??
|
| Unfortunately I only found language references, how to code guides and
| alike. Nothing that states what is really happening with my piece of
| code when I call ‘ruby myfile.rb’
|
| If you guys know some sources of such pdfs, books, articles, please let
| me know.

The Confreaks recorded a couple of presentations dealing with Ruby’s
internals (the panels on the different Ruby implementations may shed
some light on them, in particular), as well as C extensions (all for
Ruby 1.8.x).

While not necessarily quotable (Rubyconf and Mountain West Ruby Con
don’t publish proceedings, alas), they should at least give you an
overview on what to look for, and how to approach your paper.

An there is, of course, the Ruby C code (which you’ll need to refer to,
if you need to get to the nitty gritty details of implementations), the
JRuby Java code, and, last but not least, the IronRuby C# code you can
tackle, as well as Rubinius’ code.

Note, that IronRuby isn’t yet complete, and neither is Rubinius (to my
knowledge).

Rubinius is somewhat unique in this set, as it aims to make Ruby
self-hosting and thus aims to implement Ruby in Ruby.

I hope that helps. :slight_smile:


Phillip G.
Twitter: twitter.com/cynicalryan

You, you, and you panic. The rest of you, come with me.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkgESz8ACgkQbtAgaoJTgL8yAQCdGzc4WjMmYvnzhx1/B+1CG2CK
gSEAnjwGsCNN2j62kaLudy2iimNFNCRn
=VeaZ
-----END PGP SIGNATURE-----

C comes in to play in a few places - Ruby was made using C, some
libraries
are written in C and Ruby can be extended with it. Can’t find any papers
on
it though.

On Tue, Apr 15, 2008 at 3:27 AM, Paul Mueller <

Sam, Julian, Justin, Antonio, Bill, Phillip

Thanks for your instant answers. You guys built a good base to start
from.

Have a good one