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

Introduction to Ruby

Introduction to Ruby

4a25d6bbb0e5ea5e1cf777fa6ede1102?s=128

Ryan L. Cross

March 15, 2012
Tweet

More Decks by Ryan L. Cross

Other Decks in Programming

Transcript

  1. Making Ruby Do Things How to force Ruby into submission

    Wednesday, September 19, 12
  2. 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
  3. 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
  4. Mac Open Terminal.app Window Do a thing? Web http://tryruby.org Want

    to play along? Wednesday, September 19, 12
  5. Wednesday, September 19, 12

  6. The Basics Wednesday, September 19, 12

  7. Rule #1 Everything is an object Wednesday, September 19, 12

  8. 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
  9. 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
  10. Wednesday, September 19, 12

  11. Arrays people = [ 'Kirk', 'Andy', 'Jamie' ] people[2] =>

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

    ], bob: [ 'Illustrator', 'Photoshop' ] } skillz[:ryan][0] => Ruby on Rails Wednesday, September 19, 12
  13. 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
  14. 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
  15. Classes and Methods A little hot object-oriented action Wednesday, September

    19, 12
  16. There are all kinds of objects Wednesday, September 19, 12

  17. Methods def greet(name) puts "Hello, #{name}!" end Wednesday, September 19,

    12
  18. Classes class Human def greet(name) puts "Hello, #{name}!" end end

    Wednesday, September 19, 12
  19. Putting it all together class Human def greet(name) puts "Hello,

    #{name}!" end end human = Human.new human.greet('Ryan') Wednesday, September 19, 12
  20. Stop. Demo time! cylen.cc/F47w Wednesday, September 19, 12

  21. Thanks! • Ryan L. Cross • Twitter: @slant • Github:

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