RUBY                  Felipe Schmitt
an overall approach   PPRO@FEUP 2012
@schmittfelipe
CONTENTS

•   Ruby’s history

•   What is Ruby?

•   Programming paradigms insight

•   How it works?

•   Ruby’s usage

•   Where can I learn more?
RUBY’S HISTORY
Timeline: 1993 - 2000


Yukihiro Matsumoto
•   Created in 1993

•   Popular only in Japan

•   All documentation written in Japanese
Timeline: 2000 - 2004

•   First english language book published in
    2000

•   Ruby has gained a lot of interest in Agile
    Development community but still
    unknown elsewhere
Timeline: 2004 - Today

•   Ruby on Rails, the framework that
    allowed ruby to step up to the spotlight

•   Ruby v1.9

•   Core 100% documented

•   96 standard libraries
WHAT IS RUBY?
“I wanted a scripting language that was more powerful than Perl, and more object-
oriented than Python. That's why I decided to design my own language.”
                                                                            -Yukihiro Matsumoto
INFLUENCES

        Lisp

 Python
                               Ruby
CLU     Perl
      Smalltalk
WHAT IS RUBY?



•A dynamic, open source programming language with a focus on simplicity
 and productivity. It has an elegant syntax that is natural to read and easy to
 write.
PROGRAMMING PARADIGMS
Multi-paradigm programming language

                    • Functional


                    • Object   oriented

                    • Imperative


                    • Reflective




It also has a dynamic type system and automatic memory management;
HOW IT WORKS?
VS
Getting Started


HelloWorld.java

class HelloWorldApp {
   public static void main(String[] args) {
      System.out.println("Hello World!");
   }
}
Getting Started


HelloWorld.rb


    puts “Hello World!”




                          No main statement


                          No semicolons

                          ( )’s are optional
Object

Java

       String s = String.ValueOf(1);
Object

Java

       String s = String.ValueOf(1);




Ruby

              s = 1.to_s
Object

Java

       String s = String.ValueOf(1);




Ruby

              s = 1.to_s

Everything is an object!
Types

Java

public void foo(ArrayList list) {
  list.add(“foo”);
  }
Types

Java

public void foo(ArrayList list) {
  list.add(“foo”);
  }



Ruby

def foo list
  list << “foo”
end
Types

                  Java

                  public void foo(ArrayList list) {
                    list.add(“foo”);
                    }



                  Ruby
                                     (?) return
                  def foo list
                    list << “foo”
                  end
(?) Object type
Types
If list is a string: ‘foo’

If list is an array: [‘foo’]

If list is a stream: foo is written to stream


Ruby

def foo list
  list << “foo”
end
Duck Typing




“If it walks like a duck and quacks like a duck, it must be a duck.”

                                      - Pragmatic Dave Thomas
Iterators
  Objects manage their own transversal

       Ruby

       array.each { |item|
         puts item
         }




No for loops:
     Loops                   N + 1 errors
Open classes
   Ruby

     class Array
       def average
        inject do |sum, var|
            sum + var
          end / size
       end
     end

     nums = [1,2,3,4,5]
     puts nums.average                  #prints: 3

Existing classes can be modified at any time.
WHO’S USING RUBY?
RECAP

Influences                             Style                   Overall
•   Perl’s syntax                     •   Simplicity          •   Everything is an object
•   Smalltalk’s semantics             •   Productivity        •   Flexible
•   Python’s design philosophy        •   Elegant syntax      •   Large standard library




                    “Actually, I'm trying to make Ruby natural, not simple.”
                                                                      - Matz
MORE STUFF
                                      Books
              Beginning Ruby: From Novice to Professional (2009)
                      The Expert’s Voice by Peter Cooper

                       The Ruby Programming Language (2008)
                   O’reilly by David Flanagan and Yukihiro Matsumoto

                Agile Web Development with Rails (2011) 4th edition
  Pragmatic Programmers by Sam Ruby, Dave Thomas and David Heinemeier Hansson

                                       Web
                               http://www.ruby-lang.org/
             http://www.slideshare.net/mbowler/ruby-for-java-programmers
http://www.slideshare.net/vishnu/the-top-10-reasons-the-ruby-programming-language-sucks
                      http://www.fincher.org/tips/Languages/Ruby/

Ruby an overall approach

  • 1.
    RUBY Felipe Schmitt an overall approach PPRO@FEUP 2012
  • 2.
  • 3.
    CONTENTS • Ruby’s history • What is Ruby? • Programming paradigms insight • How it works? • Ruby’s usage • Where can I learn more?
  • 4.
  • 5.
    Timeline: 1993 -2000 Yukihiro Matsumoto • Created in 1993 • Popular only in Japan • All documentation written in Japanese
  • 6.
    Timeline: 2000 -2004 • First english language book published in 2000 • Ruby has gained a lot of interest in Agile Development community but still unknown elsewhere
  • 7.
    Timeline: 2004 -Today • Ruby on Rails, the framework that allowed ruby to step up to the spotlight • Ruby v1.9 • Core 100% documented • 96 standard libraries
  • 8.
  • 9.
    “I wanted ascripting language that was more powerful than Perl, and more object- oriented than Python. That's why I decided to design my own language.” -Yukihiro Matsumoto
  • 10.
    INFLUENCES Lisp Python Ruby CLU Perl Smalltalk
  • 11.
    WHAT IS RUBY? •Adynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
  • 12.
  • 13.
    Multi-paradigm programming language • Functional • Object oriented • Imperative • Reflective It also has a dynamic type system and automatic memory management;
  • 14.
  • 15.
  • 16.
    Getting Started HelloWorld.java class HelloWorldApp{ public static void main(String[] args) { System.out.println("Hello World!"); } }
  • 17.
    Getting Started HelloWorld.rb puts “Hello World!” No main statement No semicolons ( )’s are optional
  • 18.
    Object Java String s = String.ValueOf(1);
  • 19.
    Object Java String s = String.ValueOf(1); Ruby s = 1.to_s
  • 20.
    Object Java String s = String.ValueOf(1); Ruby s = 1.to_s Everything is an object!
  • 21.
    Types Java public void foo(ArrayListlist) { list.add(“foo”); }
  • 22.
    Types Java public void foo(ArrayListlist) { list.add(“foo”); } Ruby def foo list list << “foo” end
  • 23.
    Types Java public void foo(ArrayList list) { list.add(“foo”); } Ruby (?) return def foo list list << “foo” end (?) Object type
  • 24.
    Types If list isa string: ‘foo’ If list is an array: [‘foo’] If list is a stream: foo is written to stream Ruby def foo list list << “foo” end
  • 25.
    Duck Typing “If itwalks like a duck and quacks like a duck, it must be a duck.” - Pragmatic Dave Thomas
  • 26.
    Iterators Objectsmanage their own transversal Ruby array.each { |item| puts item } No for loops: Loops N + 1 errors
  • 27.
    Open classes Ruby class Array def average inject do |sum, var| sum + var end / size end end nums = [1,2,3,4,5] puts nums.average #prints: 3 Existing classes can be modified at any time.
  • 28.
  • 30.
    RECAP Influences Style Overall • Perl’s syntax • Simplicity • Everything is an object • Smalltalk’s semantics • Productivity • Flexible • Python’s design philosophy • Elegant syntax • Large standard library “Actually, I'm trying to make Ruby natural, not simple.” - Matz
  • 31.
    MORE STUFF Books Beginning Ruby: From Novice to Professional (2009) The Expert’s Voice by Peter Cooper The Ruby Programming Language (2008) O’reilly by David Flanagan and Yukihiro Matsumoto Agile Web Development with Rails (2011) 4th edition Pragmatic Programmers by Sam Ruby, Dave Thomas and David Heinemeier Hansson Web http://www.ruby-lang.org/ http://www.slideshare.net/mbowler/ruby-for-java-programmers http://www.slideshare.net/vishnu/the-top-10-reasons-the-ruby-programming-language-sucks http://www.fincher.org/tips/Languages/Ruby/