1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language...

21
1 enkat Subramaniam – [email protected] enkat Subramaniam – [email protected] Getting Started with Ruby

description

3 History Created by Yukihiro Matsumoto (Matz) in 1993 Slowly being accepted in the west Popularized by some key players in the industry Gaining strength in various applications –Ruby-On-Rails (ROR)

Transcript of 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language...

Page 1: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

1Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Getting Started with Ruby

Page 2: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

2Venkat Subramaniam – [email protected] Subramaniam – [email protected]

What’s Ruby?• Ruby is an OO, dynamic, agile language

– Everything’s an object• For example, try puts -1.abs

– Derives strengths of Perl, Smalltalk, Python• Created by Matz in mid 90’s• Prevalent in Japan; recent buzz in the US

• It’s a lightweight language– Clean, Higher signal-to-noise ratio

• There is no compiler to stand in your way• Quite flexible–you’ll see when you use it

Page 3: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

3Venkat Subramaniam – [email protected] Subramaniam – [email protected]

History• Created by Yukihiro Matsumoto (Matz) in

1993

• Slowly being accepted in the west

• Popularized by some key players in the industry

• Gaining strength in various applications– Ruby-On-Rails (ROR)

Page 4: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

4Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Downloading Ruby• Ruby site

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

• For windows, you may use one-click installer http://rubyinstaller.rubyforge.org

Page 5: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

5Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Ruby Version• You can find the version of ruby you’re

using by typing ruby –v

Page 6: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

6Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Running Ruby• Run ruby and type commands

• Interactively using irb

• Save code to file and run it

• I’ll run it directly from notepad2

• Other tools and Plugins available as well

Page 7: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

7Venkat Subramaniam – [email protected] Subramaniam – [email protected]

IDEs• Comes with FreeRIDE IDE

• Mondrian IDE

• TextPad generally used on Mac

• Command line and Notepad(2) works great as well!– The following blog entry shows you how– http://tinyurl.com/as8z7

Page 8: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

8Venkat Subramaniam – [email protected] Subramaniam – [email protected]

• ri

• RDoc at http://www.ruby-doc.org

Documentation…

Page 9: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

9Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Object-Oriented• Ruby treats almost everything as objects

• nil is an object (try: puts nil.methods)• Even classes are objects!

– You use them like objects– These objects simply represent their class

Pizza is a const thatreferences Pizza class object

Page 10: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

10Venkat Subramaniam – [email protected] Subramaniam – [email protected]

• Code less, do more

• Java:

• Ruby

Less Clutter

GOJ

Page 11: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

11Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Less Clutter• Clean language• Less noise• ; is not needed• Parenthesis are optional• Variables don’t have type specification• Last statement of a method returns

automatically• You can even return multiple values!

Page 12: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

12Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Variables• Dynamic typing

• Output:

Page 13: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

13Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Types• Everything is an object

Page 14: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

14Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Writing a function

Page 15: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

15Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Another function

Returns an array

Assigns elements of array to individual variables

Page 16: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

16Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Ruby Conventions• Conventions are very important

• Sometimes convention simply makes code look better

• At other times, it means something significant [you will learn some conventions the hard way ]

Page 17: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

17Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Class/Method conventions• Class names, modules names, and

constants start with upper case– SportsCar, ValueOfPI, DAYS_IN_WEEK

• Methods and variables start with lowercase and use underscore– start_engine(), miles_driven

• Instance variables have @– @fuel_level

• Class variables use double ats– @@suggested_pressure

• Global variables use $– $ozone

Page 18: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

18Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Method convention talks to you• You can figure out what a method is up to

from its looks

• Methods may end with an =– the method may appear as l-value

Query method

Doesn’t modify objectHas side effect;mutates object

Page 19: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

19Venkat Subramaniam – [email protected] Subramaniam – [email protected]

Global and Predefined Variables• This is hideous• You may be setting some global variable and

not know it (familiarize yourself)• Some predefs: $_, $0, $$, $&, $!, …

Page 20: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

20Venkat Subramaniam – [email protected] Subramaniam – [email protected]

"" vs '' (Double vs. Single Quotes)• '' is a simple string• Expressions within "" are evaluated

• Better to use ' if you don’t have expressions

Page 21: 1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.

21Venkat Subramaniam – [email protected] Subramaniam – [email protected]

More on Strings