# Ruby vs Python vs Java bytecode concept

**URL:** <https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797>\
**Category:** Ruby\
**Created:** [June 20, 2012, 4:56pm UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797 "2012-06-20T16:56:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 20, 2012, 4:56pm UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/1 "2012-06-20T16:56:42Z")

</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.

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 20, 2012, 6:07pm UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/2 "2012-06-20T18:07:30Z")

</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 R.

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 20, 2012, 6:08pm UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/3 "2012-06-20T18:08:40Z")

</div>

At Wed, 20 Jun 2012 23:56:48 +0900,  
gmspro gmspro wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 21, 2012, 3:46am UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/4 "2012-06-21T03:46:49Z")

</div>

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

> > 
> 
> 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.

irb\> require ‘ripper’; require ‘pp’  
irb\> pp Ripper.sexp(“puts 1 + 2”)  
[:program,  
[[:command,  
[:@ident, “puts”, [1, 0]],  
[:args\_add\_block,  
[[:binary, [:@int, “1”, [1, 5]], :+, [:@int, “2”, [1, 9]]]],  
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))

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:@\>==========  
> 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.

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 22, 2012, 4:39am UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/5 "2012-06-22T04:39:49Z")

</div>

On Wed, Jun 20, 2012 at 8:46 PM, Peter Z.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > > Suppose you have a file named f.rb.
> 
> irb\> require ‘ripper’; require ‘pp’  
> image:  
> [http://cs.lmu.edu/~ray/images/\*\*gcdast1.png](http://cs.lmu.edu/~ray/images/**gcdast1.png)[http://cs.lmu.edu/~ray/images/gcdast1.png](http://cs.lmu.edu/~ray/images/gcdast1.png)  
> == disasm: \<RubyVM::InstructionSequence:\<\*_compiled\>@\>=========_  
> developed specially for Ruby.
> 
> > 
> 
> –  
> WBR, Peter Z..

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?

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 22, 2012, 4:51am UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/6 "2012-06-22T04:51:32Z")

</div>

Unfortunately the asts are dumped after compile

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 21, 2012, 4:19am UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/7 "2012-06-21T04:19:00Z")

</div>

At Thu, 21 Jun 2012 10:46:05 +0900,  
Peter Z. wrote:

> > > To run this file you do:  
> > > Maybe it depends on the the system that you use to run ruby. For  
> > > irb\> pp Ripper.sexp(“puts 1 + 2”)  
> > > image: [http://cs.lmu.edu/~ray/images/gcdast1.png](http://cs.lmu.edu/~ray/images/gcdast1.png))  
> > > \<RubyVM::InstructionSequence:@\>==========  
> > > 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.

Thanks! you help me to understant the difference!

– Daniel

---

<div class="post-metadata">

**Author:** ![gmspro\_g](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/50afbb/32.png) [@gmspro\_g](https://www.ruby-forum.com/u/gmspro_g)\
**Post date:** [June 29, 2012, 9:39am UTC](https://www.ruby-forum.com/t/ruby-vs-python-vs-java-bytecode-concept/220797/8 "2012-06-29T09:39:34Z")

</div>

On 22/06/12 12:50, Ryan D. wrote:

> Unfortunately the asts are dumped after compile
> 
> On Jun 21, 2012, at 19:39, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?  
> > rubiunius
