Intro To Ruby
Upcoming SlideShare
Loading in...5
×
 

Intro To Ruby

on

  • 939 views

Ruby for Programmers, an introduction that includes conditionals, iterators and blocks

Ruby for Programmers, an introduction that includes conditionals, iterators and blocks

Statistics

Views

Total Views
939
Views on SlideShare
936
Embed Views
3

Actions

Likes
1
Downloads
16
Comments
0

2 Embeds 3

http://www.linkedin.com 2
http://www.slideshare.net 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Conditionals are key to being able to make decisions in a programleft looks like every other languageparentheses are optional in ruby make sure to do ==, = is an assignment, == is a conditional testExplain putsright is a little different…people in ruby don’t like to type…english readableA statement modifier lets you move control structures at the end of an expression.
  • Unlike some languages with the 0 and empty string! Binds more tightly than than the “not” keyword so you do need parentheses for example 1, but don’t need parentheses for example 2
  • Unless can be awkward, especially with else. Usually you’ll want to use if for conditionals with else clauses.Occasionally unless is more readable:unless something is nil
  • Alot of the time you will be using an array when you iterate over somethingAn array is just a list of items.Every spot in the list acts like a variable and you can make each spot point to a different objectW means wordsArray is a class, needs to start with capital letter
  • IRBif you go off the array it will be nil
  • Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
  • merge takes the value from the second hashmerge! changes h1
  • you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value
  • 5 is an object that is an instance of the integer classtimes is a method of the 5 objecttimes is a method on an object that is an instance of integer
  • it does the block of code three timesit is very rare that you will see a while loop in ruby...you can do the loops we did earlier, but rubyists will mock you.
  • What is a block? It is the ability to take a block of code, wrap it up in an object and pass it to a method. Then you can run the block of code within the method any time you want…sometimes twice! The result is kind of like sending a method to a method, except that a block isn’t bound to an object like a method is – it is an object. So what? Why use blocks?elegant syntax for iteratorsBecause there are some things that only blocks can do, like being passed to a method and being returned by a method.

Intro To Ruby Intro To Ruby Presentation Transcript

  • 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.
  • Ruby Language Overview
    Dynamically typed
    Interpreted
    Can be modified at runtime
    Object oriented
    Blocks & lambdas
    Nice support for Regular Expressions
  • Lets get started
    IRB: InteractiveRuBy
    >> 4
    >> 4 + 4
  • Everything is an object
    “test”.upcase
    “test”.class
    “test”.methods
  • Everything evaluates to something
    2 + 2
    (2+2).zero?
  • Methods are Messages
    thing.do(4)
    thing.do 4
    thing.send “do”, 4
  • Operators are Methods
    thing.do 4
    thing.do(4)
    thing.send “do”, 4
    1 + 2
    1.+(2)
    1.send "+", 2
  • Write your first Ruby class
  • Conditionals, Iterators & Blocks
    With Hashes & Arrays
  • Conditionals
  • Conditionals: if
    if age > 17
    puts “can vote”
    end
    if age > 17
    puts “can vote”
    else
    puts “attends school”
    end
    Statement Modifiers:
    y = 7 if x == 4
    Other Syntax:
    if x == 4 then y = 7 end
  • 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”) …
  • Unless
    “unless” provides us with another way of checking if a condition is false:
    unless superpower == nil
    status = “superhero”
    end
  • Case
    case superhero
    when “superman”
    city = “metropolis”
    when “batman”
    city = “gotham_city”
    else
    city = “central_city”
    end
  • Case Refactoring
    city = case superhero
    when “superman”
    “metropolis”
    when “batman”
    “gotham_city”
    else
    “central_city”
    end
  • Iterators
  • Iterators: Conditional Looping
    “while” allows us to loop through code while a set condition is true
    x = 1
    while x < 10
    puts x.to_s + “ iteration”
    x += 1
    end
  • 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”]
  • Accessing Array Values
    a = [ "a", "b", "c", "d", "e" ]
    a[0] #=> "a”
    a[2] #=> "c”
    a[6] #=> nil
  • Creating a Hash
    h = { "a" => 100, "b" => 200 }
    h[“a”]
    h = { 1 => “a”, “b” => “hello” }
    h[1]
  • 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}
  • 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
  • Times
    5.times{ puts “hello” }
    99.times do |beer_num|
    puts "#{beer_num} bottles of beer”
    end
  • Each
    superheroes = [“catwoman”, “batman”, “wonderwoman”]
    superheroes.each do | s |
    puts “#{ s } save me!”
    end
    wonderwoman save me!
    batman save me!
    catwoman save me!
  • Blocks
  • Blocks
    def dos_veces
    yield
    yield
    end
    dos_veces { puts "Hola” }
    Hola
    Hola
    Yield executes the block
    This is a Block!
    {
  • Yield with Parameters
    def bands
    yield(“abba”, “who”)
    end
    bands do |x,y| puts x,y
    end
    abba
    who
    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”
  • Mr. Neighborly's Humble Little Ruby BookJeremy McAnally
    The Well-Grounded RubyistCovering Ruby 1.9David A. Black
    Ruby Koansa walk along the path to enlightenmenthttp://github.com/edgecase/ruby_koans