Making Ruby Do Things
   How to force Ruby into submission
What is Ruby?

• Ruby is a dynamic, reflective, general-
  purpose object-oriented programming
  language...
• A dynamic, open source programming
  language with a focus on simplicity and
  productivity.
class Person
  def say something
   puts something
  end
end

# Create a person object
ryan = Person.new

# Have Ryan say hello!
ryan.say 'Hello there!'
Want to play along?

    Mac Open Terminal.app
     Window Do a thing?
    Web http://tryruby.org
The Basics
Rule #1
Everything is an object
Basic Data Types and Structures


 String               "Hello World"

Integer                     100

 Range                     1..50

 Array                    [1, 2, 3]

 Hash     { :ruby => 'neat', :javascript => 'rad' }

Regexp                     /d{3}/
Strings and Integers
Strings and Integers
name = 'Ryan'
Strings and Integers
name = 'Ryan'

# Comparing numbers (in a hash, by the way)
Strings and Integers
name = 'Ryan'

# Comparing numbers (in a hash, by the way)
hours[:xbox] = 473
Strings and Integers
name = 'Ryan'

# Comparing numbers (in a hash, by the way)
hours[:xbox] = 473
hours[:worked] = 3
Strings and Integers
name = 'Ryan'

# Comparing numbers (in a hash, by the way)
hours[:xbox] = 473
hours[:worked] = 3

hours[:xbox] > hours[:worked]
Strings and Integers
name = 'Ryan'

# Comparing numbers (in a hash, by the way)
hours[:xbox] = 473
hours[:worked] = 3

hours[:xbox] > hours[:worked]
=> true
Arrays

people = [ 'Kirk', 'Andy', 'Jamie' ]

people[2]
=> 'Jamie'
Arrays

people = [ 'Kirk', 'Andy', 'Jamie' ]
              0       1      2
people[2]
=> 'Jamie'
Hashes
skillz = {
  ryan: [ 'Ruby on Rails', 'HTML5' ],
  bob: [ 'Illustrator', 'Photoshop' ]
}

skillz[:ryan][0]
=> Ruby on Rails
Ranges
beers = (2..10)

beers.each do |count|
 puts case count
  when 2..3 then "#{count} beers? No problem!"
  when 4..7 then "Approaching dangerous territory."
  when 8..10 then "Seriously?? #{count} beers?"
 end
end

=> 2 beers? No problem!
=> 3 beers? No problem!
=> Approaching dangerous territory.
=> ...
Control Structures
Control Structures
if condition
  # do something
elsif condition
  # do something
else
  # do something
end
Control Structures
if condition       case myvar
  # do something    when foo then "Hello!"
elsif condition     when baz then "Good bye"
  # do something   end
else
  # do something
end
Control Structures
if condition       case myvar
  # do something    when foo then "Hello!"
elsif condition     when baz then "Good bye"
  # do something   end
else
  # do something      while condition
end                    # do something
                      end
Classes and Methods
  A little hot object-oriented action
There are all kinds of objects
Methods


def greet(name)
 puts "Hello, #{name}!"
end
Classes

class Human
  def greet(name)
   puts "Hello, #{name}!"
  end
end
Putting it all together
    class Human
      def greet(name)
       puts "Hello, #{name}!"
      end
    end

    human = Human.new
    human.greet('Ryan')
Stop.
Demo time!
cylen.cc/F47w
Thanks!

• Ryan L. Cross
• Twitter: @slant
• Github: github.com/cylence
• The Enclave: enclavecoop.com
• ExtendedZombieSlayer2000
  cylen.cc/F4vy

Introduction to Ruby