KEMBAR78
Metaprogramming in Ruby | PDF
Metaprogramming in Ruby ( II )


Thinking in Lisp in Ruby in Lisp in …




                                
                                    http://jon.is.emotionull.com
Creating a language

       Parsers from scratch
       Bison/yacc/lex 
       Metaprogramming FTW!




                                
Bow to the masters

       Lisp
       Scheme
       Forth
       Smalltalk 




                      
Thinking again the problem

       Code as data
       Self modifying code
       Dynamic adding/removing classes/functions
       Describe the problem, don't solve it!




                                
Metaprogramming in the wild

       Web frameworks (Ruby On Rails)
       A.I
       DSL
       Configuration files
       ...




                               
But, hooooow???

       Function pointers (C,C++)
       Open classes (Ruby)
       Metaclasses (Python)
       Lists (Lisp/Scheme/Clojure)




                                
Tools of the trade for Ruby

       Operators Overriding
       OpenClasses
       method_missing
       Blocks and eval
       Lambda




                                
Speed penalty – Not always

       Premature optimization
       Fast and smart VM (JVM)
       Bottleneck is somewhere else (DB)
       You are not smart enough. Deal with it!




                               
Cuby

       Implemented in ~ 10 minutes
       Looks like C
       Feels like C
       FULLY extensible
       Trivial to add new libraries
       You can use available Ruby libs
       VM ~ 25 lines!
                                 
C in Ruby

       Example

         // this is a test (example.cb)
         include ”stdio”
         main {
             printf ”Hello Mr. %s !”, ”Jon”
         }     



                                    
C in Ruby : Functions

       Example

         include ”stdio”
         plea_for_merci_to name {
           shout ”Please don't kill me” + name + ”!”
         }
         main {
             plea_for_merci_to ”Jon”
         }     
                                  
Real usage




         Prediction Modelling




                   
Prediction Modelling

       Lots of data (sometimes more than 60GB)
       Long live C, Fortran, Lisp
       Do you know OpenMP? If not, find another job
       Speed, speed, speed




                                
Predictions in Ruby?

       RubyVM is not very robust (think BIG > 10GB)
       Ruby is slow
       Ruby threading is difficult and bad
       Ruby does not SCALE!




                                
You are right!




          Thanks for watching!




                    
But ruby is slooooow

       1.9 is waaay better than 1.8
       Jruby is the King of the Hill 
       The bottleneck is not really here (hint: DB)




                                 
But ruby cannot scale!

       Threading in RubyVM is bad
       Fibers are way better 
       I love Jruby
         




                                  
Learn from the best: OpenMP

       Standard for parallel processing
       Easy to use
       Very mature library




                               
OpenMP example

       Example

         #pragma omp parallel for
          for (i = 0; i < arraySize; i++){  
               ....
               y[i] = sin( exp( cos( ­ exp( sin(x[i]) ) ) ) );
              ....
            }

                                      
Can we do this in Ruby?

       Example

         for j in 1..30
             puts "Hello from iteration " + j.to_s
               for i in 0..700000
                 c += a[i] * b[­i]
               end
               puts "I have just finished!"
          end 
                                      
In Cuby we trust

       Example
         for j in 1..30
             puts "Hello from iteration " + j.to_s
               parallel {
               for i in 0..700000
                 c += a[i] * b[­i]
               end
               puts "I have just finished!"
               }
                                         
          end 
 


    Thank for watching! Questions?

              Cuby available at: 
    http://github.com/jonromero/Cuby


         http://jon.is.emotionull.com
            jon@emotionull.com
                      

Metaprogramming in Ruby

  • 1.
  • 2.
    Creating a language  Parsers from scratch  Bison/yacc/lex   Metaprogramming FTW!    
  • 3.
    Bow to the masters  Lisp  Scheme  Forth  Smalltalk     
  • 4.
    Thinking again the problem  Code as data  Self modifying code  Dynamic adding/removing classes/functions  Describe the problem, don't solve it!    
  • 5.
    Metaprogramming in the wild  Web frameworks (Ruby On Rails)  A.I  DSL  Configuration files  ...    
  • 6.
    But, hooooow???  Function pointers (C,C++)  Open classes (Ruby)  Metaclasses (Python)  Lists (Lisp/Scheme/Clojure)    
  • 7.
    Tools of the trade for Ruby  Operators Overriding  OpenClasses  method_missing  Blocks and eval  Lambda    
  • 8.
    Speed penalty – Not always  Premature optimization  Fast and smart VM (JVM)  Bottleneck is somewhere else (DB)  You are not smart enough. Deal with it!    
  • 9.
    Cuby  Implemented in ~ 10 minutes  Looks like C  Feels like C  FULLY extensible  Trivial to add new libraries  You can use available Ruby libs  VM ~ 25 lines!    
  • 10.
    C in Ruby  Example // this is a test (example.cb) include ”stdio” main {     printf ”Hello Mr. %s !”, ”Jon” }         
  • 11.
    C in Ruby : Functions  Example include ”stdio” plea_for_merci_to name {   shout ”Please don't kill me” + name + ”!” } main {     plea_for_merci_to ”Jon” }         
  • 12.
    Real usage Prediction Modelling    
  • 13.
    Prediction Modelling  Lots of data (sometimes more than 60GB)  Long live C, Fortran, Lisp  Do you know OpenMP? If not, find another job  Speed, speed, speed    
  • 14.
    Predictions in Ruby?  RubyVM is not very robust (think BIG > 10GB)  Ruby is slow  Ruby threading is difficult and bad  Ruby does not SCALE!    
  • 15.
    You are right! Thanks for watching!    
  • 16.
    But ruby is slooooow  1.9 is waaay better than 1.8  Jruby is the King of the Hill   The bottleneck is not really here (hint: DB)    
  • 17.
    But ruby cannot scale!  Threading in RubyVM is bad  Fibers are way better   I love Jruby      
  • 18.
    Learn from the best: OpenMP  Standard for parallel processing  Easy to use  Very mature library    
  • 19.
    OpenMP example  Example #pragma omp parallel for  for (i = 0; i < arraySize; i++){         ....       y[i] = sin( exp( cos( ­ exp( sin(x[i]) ) ) ) );      ....    }    
  • 20.
    Can we do this in Ruby?  Example for j in 1..30     puts "Hello from iteration " + j.to_s       for i in 0..700000         c += a[i] * b[­i]       end       puts "I have just finished!"  end     
  • 21.
    In Cuby we trust  Example for j in 1..30     puts "Hello from iteration " + j.to_s       parallel {       for i in 0..700000         c += a[i] * b[­i]       end       puts "I have just finished!"       }      end 
  • 22.
      Thank for watching! Questions? Cuby available at:  http://github.com/jonromero/Cuby http://jon.is.emotionull.com jon@emotionull.com