Ruby An Introduction

Post on 13-May-2015

1.740 views 1 download

Tags:

description

My Ruby Presentation on FossConf 2008,

Transcript of Ruby An Introduction

Ruby – An IntroductionT.Shrinivasantshrinivasan@gmail.com

Indian Linux User Group, Chennai

FossConf'08

Ruby is a Programming Language

There are so many 

Programming Languages.

Why Ruby?

Ruby is simple and beautiful

Ruby is Easy to Learn

Ruby is Free Open Source Software

Ruby on Rails – Web Framework

RAA● Ruby Application Archive● 1648 projects

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

Rubyforge● 5070 projects and libraries

● http://rubyforge.org/

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

● Games● NLP

● ...

History

RubyYukihiro “Matz” Matsumoto

JapanFebruary 24, 1993

 Perl  Java  Python  Ruby  PHP1987       1991         1993  1995

        What is Ruby?

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.

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

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

Examples!

5.times { print “Ruby! " }

Ruby! Ruby! Ruby! Ruby! Ruby!

5.times { print “Ruby! " }

Everything isan object

100.next

100.next101

“I love Ruby” .reverse.capitalize

“I love Ruby”

.reverse.capitalize

“Ybur evol i”

3.hours.from_now

3.hours.from_now

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

Conventions

Variables

colored_index_cards

Class Names

Person

Symbols

:street_name

Instance Variables

@school_name

Constants

Kilograms_Per_Pound

Input

puts “What is Your name?”

name = gets

name = name.chomp

puts "Hello" + name + " .Welcome"

            Flow

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

puts “PASS” if mark > 35

                    Loop

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

end

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

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

end

Blocks

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

12345

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

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

Array

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

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

>> numbers[0]

=> "zero"

>> numbers[4]

=> "four"

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

>> numbers[3].upcase

=> "THREE"

>> numbers[3].reverse

=> "eerht"

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

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

primes.sort

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

primes.sort

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

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

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

names.sort

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

names.sort

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

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

Hashes

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

menu[:idly]2.50

Methods

Methods

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

puts say_hello(“world”)

Methods

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

puts say_hello(“world”)

Class

Classesclass MathWhiz

def say_square(value) puts value * value

end

end

sam = MathWhiz.new

sam.say_square(5)

Inheritance

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

Module

Modules

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

Modules

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

Attributes

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

Scopeclass Poet #public by default def poetry end

protected def family_legacy end

private def hopes_and_dreams endend

THE ENDof code :-)

How to Learn?

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

Web sites

Web Sites

http://ruby­lang.org

www.ruby­lang.org

www.rubyforge.net

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

Training Centers

Ruby User Groups

chennairb@googlegroups.com

● Mailing List● Meetings● Tutorial Sessions

Join Today

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