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

Ruby 101

Ruby 101

An introduction to the Ruby programming language, presented at Rails Girls Dallas 2012.

Cameron Desautels

December 08, 2012
Tweet

More Decks by Cameron Desautels

Other Decks in Programming

Transcript

  1. What is Ruby? •A programming language — a precise way

    of telling the computer what we want it to do. •Optimized for developer happiness.
  2. What is Ruby? 3.times { print “I love Ruby! ”

    } I love Ruby! I love Ruby! I love Ruby!
  3. Make a Desktop Application Bowline, a framework for making cross

    platform desktop applications in Ruby, HTML and JavaScript.
  4. Revolutionize Fashion Amisha Gadani’s Porcupine Dress: “hand-painted quills prick up

    when the wearer detects a threat” http://amishagadani.com/Work/porcupine/index.html
  5. Ruby & Rails •Ruby on Rails is a framework for

    writing web applications in Ruby. •Rails is written in Ruby and so are our Rails applications.
  6. Objects •Things in Ruby are objects. •These things come in

    types called classes. •e.g. Strings, Integers, Arrays, ActiveRecord::Models, etc.
  7. Methods •Sometimes methods take parameters (a.k.a arguments). •Parameters are (optionally)

    surrounded by parentheses. "I like broccoli".sub("broccoli", "candy") => "I like candy"
  8. Kernel Methods •A small set of basic methods available everywhere.

    gets # read a string from the terminal puts "somethings" # put (print) a string to terminal exit # exit the program
  9. Methods •It’s fun and easy to define our own: class

    String def praise self + " is wonderful" end end "chunky bacon".praise => "chunky bacon is wonderful"
  10. Variables •Names we give to things. •Places to store things

    we’ll want later. name = "Cameron" => "Cameron" greeting = "Hello, " + name => "Hello, Cameron" SPEED_OF_LIGHT = 299792458 # m/s
  11. Strings •Bits of text (words, sentences, paragraphs, etc.). "rail".length =>

    4 "rail".reverse => "liar" "rail".capitalize => "Rail" "rail" + "s girls" => "rails girls" "rails girls".split(" ") => ["rails", "girls"]
  12. Arrays •Lists of things. orgsync_devs = ["Cameron", "Kevin", "Clifton", "Aaron",

    "Cody", "Sonny", "Taylor", "Justin"] => ["Cameron", "Kevin", "Clifton", "Aaron", "Cody", "Sonny", "Taylor", "Justin"] orgsync_devs.count => 8 orgsync_devs.sort.join(", ") => => "Aaron, Cameron, Clifton, Cody, Justin, Kevin, Sonny, Taylor" orgsync_devs[2] => "Clifton"
  13. Hashes •Key / value associations. book = { "title" =>

    "Harry Potter and the Sorcerer's Stone", "author" => "J.K. Rowling", "page_count" => 320} book["author"] => "J.K. Rowling" book["isbn"] = "0439708184" => "0439708184" book => {"title"=>"Harry Potter and the Sorcerer's Stone", "author"=>"J.K. Rowling", "page_count"=>320, "isbn"=>"0439708184"}
  14. Conditionals •Do something if certain conditions are true. •Possibly something

    else otherwise. def analyze_book(book) if book["page_count"].even? puts "This book has an even number of pages." else puts "This book has an odd number of pages." end end
  15. Truthiness •Everything in Ruby has a “truth value”. •Things that

    are considered false: false, nil •Things that are considered true: everything else. (1, -10,“Hello”, [], true) if true puts "This code will always run." else puts "This book will never run." end
  16. Truthiness 2.even? => true 3.even? => false 3 > 2

    => true puts "Will this get printed?" if "something"
  17. Loops •Do something repeatedly. 3.times { print “I love Ruby!

    ” } => I love Ruby! I love Ruby! I love Ruby!
  18. Loops •Do something repeatedly. 3.times do print “I love Ruby!

    ” end I love Ruby! I love Ruby! I love Ruby!
  19. Loops •Do something repeatedly—for each item in an array. ["snozberries",

    "raspberries", "schnitzel"].each do |food| print "Let's eat " + food + "! " end Let's eat snozberries! Let's eat raspberries! Let's eat schnitzel!
  20. Identification Exercise def greet(person) greeting = "Hello, " + person

    puts greeting end ["Shufen", "Inez", "Kevin"].each do |name| if name.length > 4 greet name end end # Hello, Shufen # Hello, Kevin
  21. irb / rails console •Interactive evaluation of Ruby code in

    our terminal. •Great for testing things out.
  22. The Ruby Community •Open source — unrestricted access to other

    people’s code •GitHub(.com) — a central location where Rubyists (and others) share code •Gems — prepackaged, reusable bits of Ruby functionality