page 1 ruby by tim hanson & mamadou seck. page 2 philosophy "i hope to see ruby help every...

24
Page 1 Ruby by Tim Hanson & Mamadou Seck

Post on 15-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 1

Rubyby Tim Hanson &Mamadou Seck

Page 2: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 2

Philosophy

"I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.” - Yukihiro « Matz » Matsumoto

Page 3: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 3

History

•Created in Japan in the mid-1990s

•Christmas 1996: Version 1 released

•2000: First English book printed

•2005: Ruby on Rails popularized it

•2008: Ruby version 1.8.7 released

•2010: Ruby version 1.9.2 released (not fully compatible with Rails 3.0)

Page 4: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 4

Overview

•Influenced by Perl, Smalltalk, Eiffel, and Lisp

•Supports Multiple Programming Paradigms• Functional• Object-Oriented• Imperative• Reflective

•Dynamic typing

•Automatic Memory Management

•Interactive Ruby Shell

•Centralized Package Management via RubyGems

Page 5: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 5

TIOBE Index

Page 6: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 6

Multiple Implementations

Ruby 1.8.7• MRI – Matz’ Ruby Interpreter• Written in C• Single pass interpreted language• Slow• Compatible with Rails 3

Just-in-time Compilation implementations• YARV, JRuby, Rubinius, Iron Ruby, HotRuby • MacRuby (ahead-of -time compilation too)

Ruby 1.9.2• Based on YARV• Unicode support• Changes broke many unmaintained gems• Rails 3 not fully supported

Page 7: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 7

Performance

Page 8: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 8

Command Line Utilities

ri – ruby index• works like man in linux

irb – interactive ruby shell

gem – RubyGem package manager• works like apt-get or yum in linux but installs

ruby packages (gems) likes rails

ruby – runs ruby scripts • ruby script.rb • shebang line : #! /usr/bin env ruby

Page 9: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 9

Identifiers and Scope

Identifiers Can’t include white space Can’t include nonprinting characters Case sensitive

First character denotes scope• $ - global variables• @ - instance variables• @@ - class variables• Lower case letter or underscore - local variables• Upper case letter – constant• Capital letter – constant (convention – all caps)• Capital letter – class (convention – CamelCase)

• Method ending with: • ? - return boolean value (convention)• ! – use with caution (convention for mutators)• = - can be invoxed using assignment syntax

Page 10: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 10

Data Types

Numeric• Integer – automatically converts between subtypes

• Fixnum • Bignum – arbitrary precision

• Float• Digit has to appear on both sides of decimal

point• i.e. can’t write just .1

• BigDecimal – arbitrary precision• Complex • Rational

No primitives – everything is an object

Page 11: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 11

Data Types Continued

Symbols• prefixed by :• Lightweight strings

Strings• Many methods for String processing• Can be single quoted or double quoted• Can use any non-alphanumeric delimiter

• %!Also a valid string! Hashes

• Key – Value pairs like Dictionaries in Python Arrays

• Untyped• Mutable• Elements don’t have to be from same class• 0 is the first element and -1 is the last element• Methods size and length both return size• Self expanding• Nil extended if assign element beyond the end

Page 12: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 12

Assignment Parallel assignment

No pre or post increment or decrement operators

Conditional Assignmentx || = “default” #assigned if x is nil or falsex && = “other” #assigned if x is NOT nil or false

Self Assignment Operators•x += 2•x - = 2•x *= 2•x /= 2•x **= 2

Page 13: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 13

Flow Control

If statement• Keywords if, then, elseif, else, end• Parentheses optional• Multiple forms

Ternary Operator

Page 14: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 14

Loops

Many Different Ways to Write Loops

Page 15: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 15

Loops Continued

One line loops use braces

Page 16: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 16

Loops Continued

More Ways to Do the Same Loop

Page 17: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 17

Loops Continued

Even More Ways

Page 18: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 18

Example Programs

Page 19: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 19

Readability

Pros Identifier naming conventions denote scope Methods ending in ? are boolean Methods ending in ! are dangerous Braces used for one liners Begin and End used for blocks Doesn’t require semicolons

Cons Many different ways to do the same thing Precedence in arguments to function calls

can be confusing because () are optional There are no keywords or reserved words

- You can override everything

Page 20: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 20

Writability

Pros Many ways to do the same thing

Dynamic Typing

Can write really compact code

Functional Programming is powerful

Debugging is easy with interpreter

Extensive Libraries Available

Cons None

Page 21: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 21

Reliability

Pros Supports Exception Handling

Interpreter allows for rapid testing

Cons Dynamically Typed

Allows Aliasing

Many third party libraries are not maintained

Multiple language implementations that do not work exactly the same

Page 22: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 22

Cost

Pros Open Source

Free Compilers

Rapid Development Cycle

Netbeans adds IDE support

Feature multiplicity makes learning basics of Ruby easy for new developers

Cons Mastering all of Ruby takes a long time

Slow Execution you might have to rewrite your code

in a new language at some point

Page 23: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 23

Who uses Ruby?

AmazonBasecamp

BBCCISCO

EAFunnyordie.com

IBMJP Morgan

LucentNASA

OakleyPenny-arcade.com

Pitchforkmedia.comOracle

Scribd.comSiemensTwitterYahoo!

Page 24: Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,

Page 24

References

en.wikipedia.org/wiki/Ruby_%28programming_language%29

BLOG.OBIEFERNANDEZ.COM

www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators/

www.ruby.org/

ruby-lang.org