Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Ruby

Introduction to Ruby

Avatar for Ryan L. Cross

Ryan L. Cross

March 15, 2012
Tweet

More Decks by Ryan L. Cross

Other Decks in Programming

Transcript

  1. 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. Wednesday, September 19, 12
  2. class Person def say something puts something end end #

    Create a person object ryan = Person.new # Have Ryan say hello! ryan.say 'Hello there!' Wednesday, September 19, 12
  3. 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}/ Wednesday, September 19, 12
  4. Strings and Integers name = 'Ryan' # Comparing numbers (in

    a hash, by the way) hours[:xbox] = 473 hours[:worked] = 3 hours[:xbox] > hours[:worked] => true Wednesday, September 19, 12
  5. Arrays people = [ 'Kirk', 'Andy', 'Jamie' ] people[2] =>

    'Jamie' 0 1 2 Wednesday, September 19, 12
  6. Hashes skillz = { ryan: [ 'Ruby on Rails', 'HTML5'

    ], bob: [ 'Illustrator', 'Photoshop' ] } skillz[:ryan][0] => Ruby on Rails Wednesday, September 19, 12
  7. 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. => ... Wednesday, September 19, 12
  8. Control Structures if condition # do something elsif condition #

    do something else # do something end case myvar when foo then "Hello!" when baz then "Good bye" end while condition # do something end Wednesday, September 19, 12
  9. Putting it all together class Human def greet(name) puts "Hello,

    #{name}!" end end human = Human.new human.greet('Ryan') Wednesday, September 19, 12
  10. Thanks! • Ryan L. Cross • Twitter: @slant • Github:

    github.com/cylence • The Enclave: enclavecoop.com • ExtendedZombieSlayer2000 cylen.cc/F4vy Wednesday, September 19, 12