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

Ruby Basics: Ruby in 100 minutes or less

Liz
January 25, 2014

Ruby Basics: Ruby in 100 minutes or less

A brief overview and introduction to programming with Ruby.

Liz

January 25, 2014
Tweet

More Decks by Liz

Other Decks in Programming

Transcript

  1. hey girl hey! um, who are you? I am a

    very nice person who loves tacos.
  2. Exercises! • Some slides will have bullets on them. •

    These bullets will tell you what to do. • You should listen to the bullets... • or you won’t learn anything. • Who run the world? • Girls. Beyoncé. Me. Bullets.
  3. irb your file $ irb $ puts “hello world” #

    => “hello world” $ exit ! ! # hello.rb puts “hello taco” ! $ ruby hello.rb # => “hello taco”
  4. ruby can math! # hello.rb a = 1 b =

    2 c = a + b puts a + b puts c $ ruby hello.rb # => 3 # => 3
  5. what is d? a = 5 b = 10 c

    = 2 d = c * a - b what is d?
  6. exercise: such math • Assign some numbers to variables and

    do some math. • Write code in irb or save your file and run with terminal. • What happens when you combine operators? • What happens when you use decimals? • Do you get any unexpected results? * * try and blow it up
  7. modulo # hello.rb puts 10 % 5 puts 10 %

    3 ! $ ruby hello.rb # => 0 # => 1
  8. strings # hello.rb first_name = ‘Liz’ last_name = ‘Abinante’ name

    = first_name + ‘ ’ + last_name name_alt = “#{first_name} ‘The Taco’ #{last_name}” puts name puts name_alt
  9. strings # hello.rb age = ‘28’ puts name + ‘

    is ’ + age ! ! ! $ ruby hello.rb # => Liz Abinante is 28
  10. exercise: very variable • Create the following variables and assign

    them values: • age, name, city, favorite_color, favorite_food • Use concatenation or interpolation to create new variables.* • What do you expect? What do you get? • What happens when you do this? puts name * 15 * VARIABLE-CEPTION.
  11. BANG BANG ! Strings in Ruby are mutable. Lots of

    String class methods have destructive and non-destructive counterparts. my string class method shot me dooowwwnnn.
  12. .delete & .delete! me = “Liz” me.delete(“z”) # => “Li”

    puts me #=> “Liz” ! me.delete!(“z”) #=> “Li” puts me #=> “Li”
  13. .swapcase & .swapcase! me = “Liz” me.swapcase # => “lIZ”

    puts me #=> “Liz” ! me.swapcase! #=> “lIZ” puts me #=> “lIZ”
  14. .reverse & .reverse! party = “all the time” party.reverse! #

    => “emit eht lla” puts party # => “emit eht lla”
  15. arrays things_in_tacos = [“cheese”, “guacamole”] things_in_salsa = [“lime juice”, “mangos”]

    ! ! ! ! things_in_my_belly = [things_in_salsa, things_in_tacos, “ice cream”]
  16. arrays: new items taco = [“cheese”, “guacamole”] taco << “sour

    cream” taco << “potato chips” << “jelly beans” # => [“cheese”, “guacamole”, “sour cream”, “potato chips”, “jelly beans”]
  17. nil

  18. you already know what a method is. All that stuff

    we did to strings and arrays? Those were methods.
  19. methods: return values def taco “tacos are delicious” end !

    puts taco # => “tacos are delicious”
  20. more arguments def salutations(greeting, name) “#{greeting}, #{name.upcase}.” end ! puts

    salutations(“Goodbye”, “Angel”) # => “Goodbye, ANGEL.”
  21. multi-line methods def introduce(name, food) greet = “Hello, this is

    #{name}. ” yummy = “#{name}’s favorite food is #{food}.” greet + yummy end puts introduce(“Liz”, “TACOS”) # => “Hello, this is Liz. Liz’s favorite food is TACOS.”
  22. more on return values def fail_introduce(name, food) greet = “Hello,

    this is #{name}. ” return greet yummy = “#{name}’s favorite food is #{food}.” greet + yummy end puts fail_introduce(“Liz”, “ALL THE TACOS”)
  23. exercise: write methods • Write a method that takes more

    than 1 argument and returns a sentence composed of all the arguments. • Write a method that uses modulo. • Write a multiple-line method with a return statement. • Write a method that takes an array as an argument. * * YOU SO FANCY.