# Ruby vs Python vs Java bytecode concept

**URL:** <https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727>\
**Category:** ruby-talk\
**Created:** [20 June 2012 14:56 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727 "2012-06-20T14:56:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [20 June 2012 14:56 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/1 "2012-06-20T14:56:48Z")

</div>

Hi,

I have a confusion about the process of ruby file.

Suppose you have a file named f.rb.  
To run this file you do:  
ruby f.rb

After this command what does happen?

I knew for python, python makes bytecode and java makes .class. Both are  
not human readable.

Is ruby a interpreted language or compiled language?

Can anyone please exaplain/elaborate with example about the processing  
or running a ruby file?

Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [20 June 2012 16:02 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/2 "2012-06-20T16:02:52Z")

</div>

Ruby (1.9+) internally creates bytecode that is slightly similar to  
Python's one, but it doesn't save it anywhere (it is regenerated every  
time the script is run). Ruby 1.8 just directly evaluated the source  
code, without creating bytecode form.

-- Matma Rex

---

<div class="post-metadata">

**Author:** ![Daniel3](https://avatars.discourse-cdn.com/v4/letter/d/a4c791/32.png) [@Daniel3](https://rubytalk.org/u/Daniel3)\
**Post date:** [20 June 2012 16:03 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/3 "2012-06-20T16:03:26Z")

</div>

Maybe it depends on the the system that you use to run ruby. For  
example, YARV (now ruby 1.9) is a bytecode interpreter. I suppose that  
ruby code is converted to a bytecode sintactical tree that is  
interpreted by YARV. This is different to compile C, because the  
output of GCC is a machine code and not a sintactical tree.

> **···**
>
> At Wed, 20 Jun 2012 23:56:48 +0900, gmspro gmspro wrote:
> 
> > Hi,
> > 
> > I have a confusion about the process of ruby file.
> > 
> > Suppose you have a file named f.rb.  
> > To run this file you do:  
> > ruby f.rb
> > 
> > After this command what does happen?
> > 
> > I knew for python, python makes bytecode and java makes .class. Both are  
> > not human readable.
> > 
> > Is ruby a interpreted language or compiled language?
> 
> --  
> Daniel

---

<div class="post-metadata">

**Author:** ![Peter\_Zotov](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Peter\_Zotov](https://rubytalk.org/u/Peter_Zotov)\
**Post date:** [21 June 2012 01:46 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/4 "2012-06-21T01:46:05Z")

</div>

Daniel Hernandez писал 20.06.2012 20:03:

> > Hi,
> > 
> > I have a confusion about the process of ruby file.
> > 
> > Suppose you have a file named f.rb.  
> > To run this file you do:  
> > ruby f.rb
> > 
> > After this command what does happen?
> > 
> > I knew for python, python makes bytecode and java makes .class. Both are  
> > not human readable.
> > 
> > Is ruby a interpreted language or compiled language?
> 
> Maybe it depends on the the system that you use to run ruby. For  
> example, YARV (now ruby 1.9) is a bytecode interpreter. I suppose that  
> ruby code is converted to a bytecode sintactical tree that is  
> interpreted by YARV. This is different to compile C, because the  
> output of GCC is a machine code and not a sintactical tree.

Bytecode has nothing to do with syntactical trees. Abstract syntax tree  
is a notation used to represent the \_source code\_, e.g.

> require 'ripper'; require 'pp'  
> pp Ripper.sexp("puts 1 + 2")

[:program,  
&nbsp;&nbsp;[[:command,  
&nbsp;&nbsp;&nbsp;&nbsp;[:@ident, "puts", [1, 0]],  
&nbsp;&nbsp;&nbsp;&nbsp;[:args\_add\_block,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[:binary, [:@int, "1", [1, 5]], :+, [:@int, "2", [1, 9]]]],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false]]]]

The mentioned structure can be thought of as a tree because it does not  
have any loops (if you can't imagine it, try to find inspiration in this  
image: [http://cs.lmu.edu/~ray/images/gcdast1.png\](http://cs.lmu.edu/~ray/images/gcdast1.png%5C))

Bytecode is a format used akin to machine code, but for a virtual machine  
specialized for a certain language. Nothing (except maybe common sense)  
prevents you from creating a very real, silicon processor for the YARV  
bytecode; it isn't inherently inferior to x86 machine code. Bytecode  
looks like this:

> puts RubyVM::InstructionSequence.new("puts 1 + 2").disasm

== disasm: \<RubyVM::InstructionSequence:\<compiled\>@\<compiled\>\>==========  
0000 trace 1 ( 1)  
0002 putself  
0003 putobject 1  
0005 putobject 2  
0007 opt\_plus \<ic:2\>  
0009 send :puts, 1, nil, 8, \<ic:1\>  
0015 leave

As you can see, it's very similar to assembler code, but of a kind  
developed specially for Ruby.

Ruby 1.8 walks each node of the AST and interprets them. This is very slow.  
Ruby 1.9 first converts the AST to the bytecode and then executes the bytecode.  
This still isn't very fast, but at least it is much faster than walking the AST.

> **···**
>
> > At Wed, 20 Jun 2012 23:56:48 +0900, \> gmspro gmspro wrote:
> 
> > --  
> > Daniel
> 
> --  
> &nbsp;&nbsp;&nbsp;WBR, Peter Zotov.

---

<div class="post-metadata">

**Author:** ![Daniel3](https://avatars.discourse-cdn.com/v4/letter/d/a4c791/32.png) [@Daniel3](https://rubytalk.org/u/Daniel3)\
**Post date:** [21 June 2012 02:18 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/5 "2012-06-21T02:18:18Z")

</div>

Thanks! you help me to understant the difference!

-- Daniel

> **···**
>
> At Thu, 21 Jun 2012 10:46:05 +0900, Peter Zotov wrote:
> 
> > Daniel Hernandez писал 20.06.2012 20:03:  
> > \> At Wed, 20 Jun 2012 23:56:48 +0900, \> \> gmspro gmspro wrote:  
> > \>\>  
> > \>\> Hi,  
> > \>\>  
> > \>\> I have a confusion about the process of ruby file.  
> > \>\>  
> > \>\> Suppose you have a file named f.rb.  
> > \>\> To run this file you do:  
> > \>\> ruby f.rb  
> > \>\>  
> > \>\> After this command what does happen?  
> > \>\>  
> > \>\> I knew for python, python makes bytecode and java makes .class. Both  
> > \>\> are  
> > \>\> not human readable.  
> > \>\>  
> > \>\> Is ruby a interpreted language or compiled language?  
> > \>  
> > \> Maybe it depends on the the system that you use to run ruby. For  
> > \> example, YARV (now ruby 1.9) is a bytecode interpreter. I suppose  
> > \> that  
> > \> ruby code is converted to a bytecode sintactical tree that is  
> > \> interpreted by YARV. This is different to compile C, because the  
> > \> output of GCC is a machine code and not a sintactical tree.
> > 
> > Bytecode has nothing to do with syntactical trees. Abstract syntax tree  
> > is a notation used to represent the \_source code\_, e.g.
> > 
> > \> require 'ripper'; require 'pp'  
> > \> pp Ripper.sexp("puts 1 + 2")  
> > [:program,  
> > &nbsp;&nbsp;[[:command,  
> > &nbsp;&nbsp;&nbsp;&nbsp;[:@ident, "puts", [1, 0]],  
> > &nbsp;&nbsp;&nbsp;&nbsp;[:args\_add\_block,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[:binary, [:@int, "1", [1, 5]], :+, [:@int, "2", [1, 9]]]],  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;false]]]]
> > 
> > The mentioned structure can be thought of as a tree because it does not  
> > have any loops (if you can't imagine it, try to find inspiration in  
> > this  
> > image: [http://cs.lmu.edu/~ray/images/gcdast1.png\](http://cs.lmu.edu/~ray/images/gcdast1.png%5C))
> > 
> > Bytecode is a format used akin to machine code, but for a virtual  
> > machine  
> > specialized for a certain language. Nothing (except maybe common sense)  
> > prevents you from creating a very real, silicon processor for the YARV  
> > bytecode; it isn't inherently inferior to x86 machine code. Bytecode  
> > looks like this:
> > 
> > \> puts RubyVM::InstructionSequence.new("puts 1 + 2").disasm  
> > == disasm:  
> > \<RubyVM::InstructionSequence:\<compiled\>@\<compiled\>\>==========  
> > 0000 trace 1 (  
> > &nbsp;&nbsp;1)  
> > 0002 putself  
> > 0003 putobject 1  
> > 0005 putobject 2  
> > 0007 opt\_plus \<ic:2\>  
> > 0009 send :puts, 1, nil, 8, \<ic:1\>  
> > 0015 leave
> > 
> > As you can see, it's very similar to assembler code, but of a kind  
> > developed specially for Ruby.
> > 
> > Ruby 1.8 walks each node of the AST and interprets them. This is very  
> > slow.  
> > Ruby 1.9 first converts the AST to the bytecode and then executes the  
> > bytecode.  
> > This still isn't very fast, but at least it is much faster than walking  
> > the AST.

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [22 June 2012 02:39 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/6 "2012-06-22T02:39:13Z")

</div>

Holy crap, I didn't even know about RubyVM.

Is there a way to get at the code from a block passed in? Looking at the  
docs, it appears both of these take a string, Could I do this sort of  
analysis and manipulation on ASTs or would I need to drop into C for that?

> **···**
>
> On Wed, Jun 20, 2012 at 8:46 PM, Peter Zotov \<whitequark@whitequark.org\>wrote:
> 
> > Daniel Hernandez писал 20.06.2012 20:03:
> > 
> > At Wed, 20 Jun 2012 23:56:48 +0900, \>\> gmspro gmspro wrote:
> > 
> > > > Hi,
> > > > 
> > > > I have a confusion about the process of ruby file.
> > > > 
> > > > Suppose you have a file named f.rb.  
> > > > To run this file you do:  
> > > > ruby f.rb
> > > > 
> > > > After this command what does happen?
> > > > 
> > > > I knew for python, python makes bytecode and java makes .class. Both are  
> > > > not human readable.
> > > > 
> > > > Is ruby a interpreted language or compiled language?
> > > 
> > > Maybe it depends on the the system that you use to run ruby. For  
> > > example, YARV (now ruby 1.9) is a bytecode interpreter. I suppose that  
> > > ruby code is converted to a bytecode sintactical tree that is  
> > > interpreted by YARV. This is different to compile C, because the  
> > > output of GCC is a machine code and not a sintactical tree.
> > 
> > Bytecode has nothing to do with syntactical trees. Abstract syntax tree  
> > is a notation used to represent the \_source code\_, e.g.
> > 
> > \> require 'ripper'; require 'pp'  
> > \> pp Ripper.sexp("puts 1 + 2")  
> > [:program,  
> > [[:command,  
> > &nbsp;&nbsp;[:@ident, "puts", [1, 0]],  
> > &nbsp;&nbsp;[:args\_add\_block,  
> > &nbsp;&nbsp;&nbsp;[[:binary, [:@int, "1", [1, 5]], :+, [:@int, "2", [1, 9]]]],  
> > &nbsp;&nbsp;&nbsp;false]]]]
> > 
> > The mentioned structure can be thought of as a tree because it does not  
> > have any loops (if you can't imagine it, try to find inspiration in this  
> > image: [http://cs.lmu.edu/~ray/images/\*\*gcdast1.png&lt;http://cs.lmu.edu/~ray/images/gcdast1.png&gt](http://cs.lmu.edu/~ray/images/**gcdast1.png&lt;http://cs.lmu.edu/~ray/images/gcdast1.png&gt);  
> > )
> > 
> > Bytecode is a format used akin to machine code, but for a virtual machine  
> > specialized for a certain language. Nothing (except maybe common sense)  
> > prevents you from creating a very real, silicon processor for the YARV  
> > bytecode; it isn't inherently inferior to x86 machine code. Bytecode  
> > looks like this:
> > 
> > puts RubyVM::InstructionSequence.\*\*new("puts 1 + 2").disasm
> > 
> > > 
> > 
> > == disasm: \<RubyVM::InstructionSequence:\<\*\*compiled\>@\<compiled\>\>=========\*  
> > \*=  
> > 0000 trace 1 ( 1)  
> > 0002 putself  
> > 0003 putobject 1  
> > 0005 putobject 2  
> > 0007 opt\_plus \<ic:2\>  
> > 0009 send :puts, 1, nil, 8, \<ic:1\>  
> > 0015 leave
> > 
> > As you can see, it's very similar to assembler code, but of a kind  
> > developed specially for Ruby.
> > 
> > Ruby 1.8 walks each node of the AST and interprets them. This is very slow.  
> > Ruby 1.9 first converts the AST to the bytecode and then executes the  
> > bytecode.  
> > This still isn't very fast, but at least it is much faster than walking  
> > the AST.
> > 
> > > --  
> > > Daniel
> > 
> > --  
> > WBR, Peter Zotov.

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [22 June 2012 02:50 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/7 "2012-06-22T02:50:33Z")

</div>

Unfortunately the asts are dumped after compile

> **···**
>
> On Jun 21, 2012, at 19:39, Josh Cheek \<josh.cheek@gmail.com\> wrote:
> 
> > Is there a way to get at the code from a block passed in? Looking at the docs, it appears both of these take a string, Could I do this sort of analysis and manipulation on ASTs or would I need to drop into C for that?

---

<div class="post-metadata">

**Author:** ![Shaun\_Lloyd](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@Shaun\_Lloyd](https://rubytalk.org/u/Shaun_Lloyd)\
**Post date:** [29 June 2012 07:38 UTC](https://rubytalk.org/t/ruby-vs-python-vs-java-bytecode-concept/65727/8 "2012-06-29T07:38:55Z")

</div>

rubiunius

> **···**
>
> On 22/06/12 12:50, Ryan Davis wrote:
> 
> > Unfortunately the asts are dumped after compile
> > 
> > On Jun 21, 2012, at 19:39, Josh Cheek \<josh.cheek@gmail.com\> wrote:
> > 
> > > Is there a way to get at the code from a block passed in? Looking at the docs, it appears both of these take a string, Could I do this sort of analysis and manipulation on ASTs or would I need to drop into C for that?
