Rochester on Rails: Introduction to Ruby

49
Ruby Programmers’ Best Friend Jason Morrison January 19, 2006 Rochester on Rails

description

A brief and basic introduction to the syntax of Ruby 1.8, along with a few nice language features. This is a talk I gave at one of our first Rochester on Rails meetings.

Transcript of Rochester on Rails: Introduction to Ruby

Page 1: Rochester on Rails: Introduction to Ruby

RubyProgrammers’ Best Friend

Jason Morrison

January 19, 2006

Rochester on Rails

Page 2: Rochester on Rails: Introduction to Ruby

History

Page 3: Rochester on Rails: Introduction to Ruby

RubyYukihiro “Matz” Matsumoto

February 24, 1993

Page 4: Rochester on Rails: Introduction to Ruby

Perl Java Python Ruby PHP1987 1991 1993 1995

Page 5: Rochester on Rails: Introduction to Ruby

Examples!

Page 6: Rochester on Rails: Introduction to Ruby

5.times { print “Ruby! " }

Page 7: Rochester on Rails: Introduction to Ruby

Ruby! Ruby! Ruby! Ruby! Ruby!

5.times { print “Ruby! " }

Page 8: Rochester on Rails: Introduction to Ruby

[‘one’,‘two’,‘three’].each {|num| puts num.capitalize}

Page 9: Rochester on Rails: Introduction to Ruby

[‘one’,‘two’,‘three’].each {|num| puts num.capitalize}

OneTwoThree

Page 10: Rochester on Rails: Introduction to Ruby

Everything isan object

Page 11: Rochester on Rails: Introduction to Ruby

-21474836480.abs

Page 12: Rochester on Rails: Introduction to Ruby

-21474836480.abs21474836480

Page 13: Rochester on Rails: Introduction to Ruby

“Rats live on no evil star”

.reverse.capitalize

Page 14: Rochester on Rails: Introduction to Ruby

“Rats live on no evil star”

.reverse.capitalize

“Rats live on no evil star”

Page 15: Rochester on Rails: Introduction to Ruby

3.hours.from_now

Page 16: Rochester on Rails: Introduction to Ruby

3.hours.from_now

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

Page 17: Rochester on Rails: Introduction to Ruby

Conventions

Page 18: Rochester on Rails: Introduction to Ruby

Variables

colored_index_cards

Page 19: Rochester on Rails: Introduction to Ruby

Class Names

DromedaryDiner

Page 20: Rochester on Rails: Introduction to Ruby

Symbols

:creme_de_menthe

Page 21: Rochester on Rails: Introduction to Ruby

Instance Variables

@euros_per_liter

Page 22: Rochester on Rails: Introduction to Ruby

Constants

Kilograms_Per_Pound

Page 23: Rochester on Rails: Introduction to Ruby

Syntax

Page 24: Rochester on Rails: Introduction to Ruby

Methods

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

puts say_hello(“world”)

Page 25: Rochester on Rails: Introduction to Ruby

Methods

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

puts say_hello(“world”)

Page 26: Rochester on Rails: Introduction to Ruby

Classesclass MathWhiz

def say_square(value) puts value * value

end

end

sam = MathWhiz.new

sam.say_square(5)

Page 27: Rochester on Rails: Introduction to Ruby

Open Classesclass Integer

def squared

self * self

end

end

5.squared #=> 25

Page 28: Rochester on Rails: Introduction to Ruby

Inheritance

class Whopper < Burger @maker = “Burger King” @calories = 0.67 * 10**3end

Page 29: Rochester on Rails: Introduction to Ruby

Class Methods

class FileUtil def self.mkdir(dir) # do it! endend

FileUtil.mkdir(“oranges”)

Page 30: Rochester on Rails: Introduction to Ruby

Modules

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

Page 31: Rochester on Rails: Introduction to Ruby

Modules

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

Page 32: Rochester on Rails: Introduction to Ruby

Mixins

module Debug def whoAmI? “#{self.type.name} ” + “(\##{self.id}): ” + “#{self.to_s}" endend

Page 33: Rochester on Rails: Introduction to Ruby

Mixins

class Phonograph include Debug # ...endph = Phonograph.new("West End Blues")ph.whoAmI?"Phonograph (#537766170): West End Blues"

Page 34: Rochester on Rails: Introduction to Ruby

Attributes

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

Page 35: Rochester on Rails: Introduction to Ruby

Scopeclass Poet #public by default def poetry end

protected def family_legacy end

private def hopes_and_dreams endend

Page 36: Rochester on Rails: Introduction to Ruby

Arrays

Page 37: Rochester on Rails: Introduction to Ruby

foo = []

foo << 1 #=> [1]foo << 2 #=> [1, 2]foo << 3 #=> [1, 2, 3]

Page 38: Rochester on Rails: Introduction to Ruby

bar = [1, 2, 3]

bar << 4 #=> [1, 2, 3, 4]bar << 5 #=> [1, 2, 3, 4, 5]bar << 6 #=> [1, 2, 3, 4, 5, 6]

Page 39: Rochester on Rails: Introduction to Ruby

folks = %w( Charles Ed Amanda )#=> [“Charles”, “Ed”, “Amanda”]

folks[1]Ed

Page 40: Rochester on Rails: Introduction to Ruby

Hashes

Page 41: Rochester on Rails: Introduction to Ruby

menu = { :douglas_sirk_steak => 17.50, :vanilla_coke => 2.75, :durwood_kirby_burger => 9.75, :five_dollar_shake => 5.00}

menu[:vanilla_coke]2.75

Page 42: Rochester on Rails: Introduction to Ruby

Flow

Page 43: Rochester on Rails: Introduction to Ruby

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

Page 44: Rochester on Rails: Introduction to Ruby

puts “Watch out!” if lion_distance < 5

Page 45: Rochester on Rails: Introduction to Ruby

Blocks

Page 46: Rochester on Rails: Introduction to Ruby

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

12345

Page 47: Rochester on Rails: Introduction to Ruby

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

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

Page 48: Rochester on Rails: Introduction to Ruby

Fín!

Page 49: Rochester on Rails: Introduction to Ruby

Homepagewww.ruby-lang.org

Try Ruby in your browsertryruby.hobix.com

Ruby with humor www.poignantguide.net