Ruby An Introduction

84
Ruby – An Introduction T.Shrinivasan [email protected] Indian Linux User Group, Chennai FossConf'08

description

My Ruby Presentation on FossConf 2008,

Transcript of Ruby An Introduction

Page 1: Ruby An Introduction

Ruby – An [email protected]

Indian Linux User Group, Chennai

FossConf'08

Page 2: Ruby An Introduction

Ruby is a Programming Language

Page 3: Ruby An Introduction

There are so many 

Programming Languages.

Why Ruby?

Page 4: Ruby An Introduction
Page 5: Ruby An Introduction
Page 6: Ruby An Introduction

Ruby is simple and beautiful

Page 7: Ruby An Introduction

Ruby is Easy to Learn

Page 8: Ruby An Introduction

Ruby is Free Open Source Software

Page 9: Ruby An Introduction

Ruby on Rails – Web Framework

Page 10: Ruby An Introduction

RAA● Ruby Application Archive● 1648 projects

● http://raa.ruby-lang.org

Page 11: Ruby An Introduction

Rubyforge● 5070 projects and libraries

● http://rubyforge.org/

Page 12: Ruby An Introduction

Can do● Text Handling● System Administration● GUI programming● Web Applications● Database Apps● Scientific Applications

● Games● NLP

● ...

Page 13: Ruby An Introduction

History

Page 14: Ruby An Introduction

RubyYukihiro “Matz” Matsumoto

JapanFebruary 24, 1993

Page 15: Ruby An Introduction

 Perl  Java  Python  Ruby  PHP1987       1991         1993  1995

Page 16: Ruby An Introduction

        What is Ruby?

Page 17: Ruby An Introduction

Ruby is…

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 18: Ruby An Introduction

Quick and Easy●      Intrepreted Scripting Language●      Variable declarations are unnecessary●      Variables are not typed●      syntax is simple and consistent●      memory management is automatic

Page 19: Ruby An Introduction

Object Oriented Programming●      Everything is an object●      classes, methods, inheritance, etc.●      singleton methods●      "mixin" functionality by module●      iterators and closures

Page 20: Ruby An Introduction

Examples!

Page 21: Ruby An Introduction

5.times { print “Ruby! " }

Page 22: Ruby An Introduction

Ruby! Ruby! Ruby! Ruby! Ruby!

5.times { print “Ruby! " }

Page 23: Ruby An Introduction

Everything isan object

Page 24: Ruby An Introduction

100.next

Page 25: Ruby An Introduction

100.next101

Page 26: Ruby An Introduction

“I love Ruby” .reverse.capitalize

Page 27: Ruby An Introduction

“I love Ruby”

.reverse.capitalize

“Ybur evol i”

Page 28: Ruby An Introduction

3.hours.from_now

Page 29: Ruby An Introduction

3.hours.from_now

Thu Jan 19 22:05:00 Eastern Standard Time 2006

Page 30: Ruby An Introduction

Conventions

Page 31: Ruby An Introduction

Variables

colored_index_cards

Page 32: Ruby An Introduction

Class Names

Person

Page 33: Ruby An Introduction

Symbols

:street_name

Page 34: Ruby An Introduction

Instance Variables

@school_name

Page 35: Ruby An Introduction

Constants

Kilograms_Per_Pound

Page 36: Ruby An Introduction

Input

Page 37: Ruby An Introduction

puts “What is Your name?”

name = gets

name = name.chomp

puts "Hello" + name + " .Welcome"

Page 38: Ruby An Introduction

            Flow

Page 39: Ruby An Introduction

if ( score >= 5000 ) puts “You win!”elsif ( score <= 0 ) puts “Game over.”else puts “Current score: #{score}”end

Page 40: Ruby An Introduction

puts “PASS” if mark > 35

Page 41: Ruby An Introduction

                    Loop

Page 42: Ruby An Introduction

count = 05.times docount += 1puts "Count =" + count.to_s

end

Page 43: Ruby An Introduction

Count = 1Count = 2Count = 3Count = 4Count = 5

Page 44: Ruby An Introduction

count = 0while count < 10puts "Count = " +count.to_scount += 1

end

Page 45: Ruby An Introduction

Blocks

Page 46: Ruby An Introduction

1.upto(5) { |x| puts x }

12345

Page 47: Ruby An Introduction

5.downto(1) do |time| print “#{time}... ” puts “!” if time <= 3end

5... 4... 3... !2... !1... !

Page 48: Ruby An Introduction

Array

Page 49: Ruby An Introduction

Arraynumbers = [ "zero", "one", "two", "three", "four" ]  

Page 50: Ruby An Introduction

Arraynumbers = [ "zero", "one", "two", "three", "four" ]  

>> numbers[0]

=> "zero"

>> numbers[4]

=> "four"

Page 51: Ruby An Introduction

Arraynumbers = [ "zero", "one", "two", "three", "four" ]

>> numbers[3].upcase

=> "THREE"

>> numbers[3].reverse

=> "eerht"

Page 52: Ruby An Introduction

Sort Arrayprimes = [ 11, 5, 7, 2, 13, 3 ]

Page 53: Ruby An Introduction

Sort Arrayprimes = [ 11, 5, 7, 2, 13, 3 ]

primes.sort

Page 54: Ruby An Introduction

Sort Arrayprimes = [ 11, 5, 7, 2, 13, 3 ]

primes.sort

=> [2, 3, 5, 7, 11, 13]

Page 55: Ruby An Introduction

Sort Arraynames = [ "Shrini", "Bala", "Suresh", "Arul"]

Page 56: Ruby An Introduction

Sort Arraynames = [ "Shrini", "Bala", "Suresh", "Arul"]

names.sort

Page 57: Ruby An Introduction

Sort Arraynames = [ "Shrini", "Bala", "Suresh", "Arul"]

names.sort

=>["Arul", "Bala", "Shrini", "Suresh"]

Page 58: Ruby An Introduction

More on Arrays● Reverse● Length● Delete● Join● Find● More than 100 methods

Page 59: Ruby An Introduction

Hashes

Page 60: Ruby An Introduction

menu = { :idly => 2.50, :dosai => 10.00, :coffee => 5.00, :ice_cream => 5.00}

menu[:idly]2.50

Page 61: Ruby An Introduction

Methods

Page 62: Ruby An Introduction

Methods

def say_hello(name) result = “Hello, #{name}!” return resultend

puts say_hello(“world”)

Page 63: Ruby An Introduction

Methods

def say_hello(name) “Hello, #{name}!”end

puts say_hello(“world”)

Page 64: Ruby An Introduction

Class

Page 65: Ruby An Introduction

Classesclass MathWhiz

def say_square(value) puts value * value

end

end

sam = MathWhiz.new

sam.say_square(5)

Page 66: Ruby An Introduction

Inheritance

class Dog < Animal @catagory = “mammal” @legs = 4end

Page 67: Ruby An Introduction

Module

Page 68: Ruby An Introduction

Modules

module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. endend

Page 69: Ruby An Introduction

Modules

require "trig"y = Trig.sin(Trig::PI/4)0.707106780551956

Page 70: Ruby An Introduction

Attributes

class PlainOldRubyObject attr_accessor :food, :drinks attr_reader :advice attr_writer :write_onlyend

Page 71: Ruby An Introduction

Scopeclass Poet #public by default def poetry end

protected def family_legacy end

private def hopes_and_dreams endend

Page 72: Ruby An Introduction

THE ENDof code :-)

Page 73: Ruby An Introduction

How to Learn?

Page 74: Ruby An Introduction

irb● interactive ruby● A ruby Shell● Instance response● learn as you type

Page 75: Ruby An Introduction
Page 76: Ruby An Introduction

Web sites

Web Sites

Page 77: Ruby An Introduction

http://ruby­lang.org

www.ruby­lang.org

Page 78: Ruby An Introduction

www.rubyforge.net

Page 79: Ruby An Introduction

http://www.ruby­forum.com/

Page 80: Ruby An Introduction

Training Centers

Page 81: Ruby An Introduction

Ruby User Groups

[email protected]

● Mailing List● Meetings● Tutorial Sessions

Join Today

Page 82: Ruby An Introduction
Page 83: Ruby An Introduction
Page 84: Ruby An Introduction

Copyright (c)  2008   Permission is granted to copy, distribute and/or modify this document  under the terms of the GNU Free Documentation License, Version 1.2

  or any later version published by the Free Software Foundation.

http://www.gnu.org/copyleft/fdl.html  

We thank                         and                               for Photos