intro to ruby

29
Ruby

Upload: sarah-allen

Post on 20-Aug-2015

633 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Intro To Ruby

Ruby

Page 2: Intro To Ruby

The Ruby Language

• Originally by Yukihiro "Matz" Matsumoto• “Ruby is designed for programmer

productivity and fun, following the principles of good user interface design. He stresses that systems design needs to emphasize human, rather than computer, needs.”

http://en.wikipedia.org/wiki/Ruby_(programming_language)#History

• Ruby 1.0 was released in 1996.

Page 3: Intro To Ruby

Ruby Language Overview

• Dynamically typed• Interpreted• Can be modified at runtime• Object oriented• Blocks & lambdas• Nice support for Regular Expressions

Page 4: Intro To Ruby

Lets get started

• IRB: InteractiveRuBy>> 4>> 4 + 4

Page 5: Intro To Ruby

Everything is an object

“test”.upcase“test”.class“test”.methods

Page 6: Intro To Ruby

Everything evaluates to something

2 + 2(2+2).zero?

Page 7: Intro To Ruby

Methods are Messages

thing.do(4)thing.do 4thing.send “do”, 4

Page 8: Intro To Ruby

Operators are Methods

thing.do 4thing.do(4)thing.send “do”, 4

1 + 21.+(2)1.send "+", 2

Page 9: Intro To Ruby

Write your first Ruby class

Page 10: Intro To Ruby

Conditionals, Iterators & Blocks

With Hashes & Arrays

Page 11: Intro To Ruby

CONDITIONALS

Page 12: Intro To Ruby

Conditionals: ifif age > 17puts “can vote”

end

if age > 17puts “can vote”

elseputs “attends school”

end

Statement Modifiers:y = 7 if x == 4

Other Syntax:if x == 4 then y = 7

end

Page 13: Intro To Ruby

Truth• Truth: Everything is true except for:– false – nil

• Therefore– 0 is true– “” is true

• Checking for false:if !(name == “superman”) …if not (name == “superman”) …

Page 14: Intro To Ruby

Unless

• “unless” provides us with another way of checking if a condition is false:

unless superpower == nilstatus = “superhero”

end

Page 15: Intro To Ruby

Case

case superherowhen “superman”

city = “metropolis”when “batman”

city = “gotham_city”else

city = “central_city”end

Page 16: Intro To Ruby

Case Refactoring

city = case superherowhen “superman”

“metropolis”when “batman”

“gotham_city”else

“central_city”end

Page 17: Intro To Ruby

ITERATORS

Page 18: Intro To Ruby

Iterators: Conditional Looping

• “while” allows us to loop through code while a set condition is true

x = 1while x < 10puts x.to_s + “ iteration”x += 1

end

Page 19: Intro To Ruby

Creating a new array

x = [1, 2, 3, 4]=> [1, 2, 3, 4]

x = %w(1 2 3 4) => [“1”, “2”, “3”, “4”]

chef = Array.new(3, “bork”)=> [“bork”, “bork”, bork”]

Page 20: Intro To Ruby

Accessing Array Values

a = [ "a", "b", "c", "d", "e" ] a[0] #=> "a”

a[2] #=> "c”

a[6] #=> nil

Page 21: Intro To Ruby

Creating a Hash

h = { "a" => 100, "b" => 200 }h[“a”]

h = { 1 => “a”, “b” => “hello” }h[1]

Page 22: Intro To Ruby

Operations on Hashes: Merge

h1 = { "a" => 100, "b" => 200 } => {"a"=>100, "b"=>200}h2 = { "b" => 254, "c" => 300 }=>{"b"=>254, "c"=>300}

h3 = h1.merge(h2)=> {"a"=>100, "b"=>254, "c"=>300}h1=> {"a"=>100, "b"=>200}

h1.merge!(h2)=> {"a"=>100, "b"=>254, "c"=>300}

Page 23: Intro To Ruby

Operations on Hashes

h = { "a" => 100, "b" => 200 } h.delete("a”)

h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }

letters = h.keys

h = { "a" => 100, "b" => 200, "c" => 300 }

numbers = h.values

Page 24: Intro To Ruby

Times

5.times{ puts “hello” }

99.times do |beer_num| puts "#{beer_num} bottles

of beer”end

Page 25: Intro To Ruby

Eachsuperheroes = [“catwoman”, “batman”, “wonderwoman”]superheroes.each do | s |

puts “#{ s } save me!” end

wonderwoman save me!batman save me!catwoman save me!

Page 26: Intro To Ruby

BLOCKS

Page 27: Intro To Ruby

Blocks

def dos_veces yield yieldend

dos_veces { puts "Hola” }

HolaHola

This is a Block!

{Yield executes the block

Page 28: Intro To Ruby

Yield with Parameters

def bandsyield(“abba”, “who”)

end

bands do |x,y| puts x,y

end

abbawho

Yield sends its parameters as arguments to the block

yield(“abba”, ”who”) sends “abba” and “who” to |x, y|

x is set to “abba”

y is set to “who”

Page 29: Intro To Ruby

Ruby Koansa walk along the path to enlightenment

http://github.com/edgecase/ruby_koans

Mr. Neighborly's Humble Little

Ruby BookJeremy McAnally

The Well-Grounded RubyistCovering Ruby 1.9

David A. Black