Ruby an overall approach

31
RUBY an overall approach Felipe Schmitt PPRO@FEUP 2012

description

An overall approach of Ruby. Presentation given at a paradigm programming course at FEUP ’12.

Transcript of Ruby an overall approach

Page 1: Ruby an overall approach

RUBYan overall approach

Felipe Schmitt PPRO@FEUP 2012

Page 2: Ruby an overall approach

@schmittfelipe

Page 3: Ruby an overall approach

CONTENTS

• Ruby’s history

• What is Ruby?

• Programming paradigms insight

• How it works?

• Ruby’s usage

• Where can I learn more?

Page 4: Ruby an overall approach

RUBY’S HISTORY

Page 5: Ruby an overall approach

Yukihiro Matsumoto

Timeline: 1993 - 2000

• Created in 1993

• Popular only in Japan

• All documentation written in Japanese

Page 6: Ruby an overall approach

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

Page 7: Ruby an overall approach

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

Page 8: Ruby an overall approach

WHAT IS RUBY?

Page 9: Ruby an overall approach

“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

Page 10: Ruby an overall approach

INFLUENCES

Smalltalk

Perl

Python

CLU

Lisp

Ruby

Page 11: Ruby an overall approach

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.

Page 12: Ruby an overall approach

PROGRAMMING PARADIGMS

Page 13: Ruby an overall approach

• Functional

• Object oriented

• Imperative

• Reflective

It also has a dynamic type system and automatic memory management;

Multi-paradigm programming language

Page 14: Ruby an overall approach

HOW IT WORKS?

Page 15: Ruby an overall approach

VS

Page 16: Ruby an overall approach

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

HelloWorld.java

Getting Started

Page 17: Ruby an overall approach

puts “Hello World!”

HelloWorld.rb

No main statement

No semicolons

Getting Started

( )’s are optional

Page 18: Ruby an overall approach

Object

String s = String.ValueOf(1);Java

Page 19: Ruby an overall approach

Object

s = 1.to_sRuby

String s = String.ValueOf(1);Java

Page 20: Ruby an overall approach

Everything is an object!

Object

String s = String.ValueOf(1);Java

s = 1.to_sRuby

Page 21: Ruby an overall approach

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

Java

Types

Page 22: Ruby an overall approach

Types

def foo listlist << “foo”

end

Ruby

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

Java

Page 23: Ruby an overall approach

Types

(?) return

(?) Object type

def foo listlist << “foo”

end

Ruby

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

Java

Page 24: Ruby an overall approach

If list is a string: ‘foo’

If list is an array: [‘foo’]

If list is a stream: foo is written to stream

Types

def foo listlist << “foo”

end

Ruby

Page 25: Ruby an overall approach

Duck Typing

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

- Pragmatic Dave Thomas

Page 26: Ruby an overall approach

Loops N + 1 errors

Iterators

No for loops:

array.each { |item| puts item }

Ruby

Objects manage their own transversal

Page 27: Ruby an overall approach

Open classes

class Array def average inject do |sum, var|

sum + varend / size

endend

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

Ruby

Existing classes can be modified at any time.

#prints: 3

Page 28: Ruby an overall approach

WHO’S USING RUBY?

Page 29: Ruby an overall approach
Page 30: Ruby an overall approach

RECAP

• Perl’s syntax

• Smalltalk’s semantics

• Python’s design philosophy

Influences• Everything is an object

• Flexible

• Large standard library

Overall• Simplicity

• Productivity

• Elegant syntax

Style

“Actually, I'm trying to make Ruby natural, not simple.”- Matz

Page 31: Ruby an overall approach

http://www.fincher.org/tips/Languages/Ruby/

MORE STUFF

http://www.slideshare.net/vishnu/the-top-10-reasons-the-ruby-programming-language-sucks

http://www.slideshare.net/mbowler/ruby-for-java-programmers

http://www.ruby-lang.org/

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

Books

Web

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

Beginning Ruby: From Novice to Professional (2009)The Expert’s Voice by Peter Cooper