Ruby Basics: Ruby in 100 minutes or less

3d65a0bc911de24fde5e58d84b0276af?s=47 Liz
January 25, 2014

Ruby Basics: Ruby in 100 minutes or less

A brief overview and introduction to programming with Ruby.

3d65a0bc911de24fde5e58d84b0276af?s=128

Liz

January 25, 2014
Tweet

Transcript

  1. 2.

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

    very nice person who loves tacos.
  2. 7.

    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. 9.

    irb your file $ irb $ puts “hello world” #

    => “hello world” $ exit ! ! # hello.rb puts “hello taco” ! $ ruby hello.rb # => “hello taco”
  4. 10.
  5. 12.
  6. 13.
  7. 14.

    ruby can math! # hello.rb a = 1 b =

    2 c = a + b puts a + b puts c $ ruby hello.rb # => 3 # => 3
  8. 15.

    what is d? a = 5 b = 10 c

    = 2 d = c * a - b what is d?
  9. 16.

    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
  10. 17.

    modulo # hello.rb puts 10 % 5 puts 10 %

    3 ! $ ruby hello.rb # => 0 # => 1
  11. 18.
  12. 19.

    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
  13. 20.
  14. 21.

    strings # hello.rb age = ‘28’ puts name + ‘

    is ’ + age ! ! ! $ ruby hello.rb # => Liz Abinante is 28
  15. 22.

    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.
  16. 24.

    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.
  17. 25.

    .delete & .delete! me = “Liz” me.delete(“z”) # => “Li”

    puts me #=> “Liz” ! me.delete!(“z”) #=> “Li” puts me #=> “Li”
  18. 26.

    .swapcase & .swapcase! me = “Liz” me.swapcase # => “lIZ”

    puts me #=> “Liz” ! me.swapcase! #=> “lIZ” puts me #=> “lIZ”
  19. 27.

    .reverse & .reverse! party = “all the time” party.reverse! #

    => “emit eht lla” puts party # => “emit eht lla”
  20. 29.
  21. 30.

    arrays things_in_tacos = [“cheese”, “guacamole”] things_in_salsa = [“lime juice”, “mangos”]

    ! ! ! ! things_in_my_belly = [things_in_salsa, things_in_tacos, “ice cream”]
  22. 33.

    arrays: new items taco = [“cheese”, “guacamole”] taco << “sour

    cream” taco << “potato chips” << “jelly beans” # => [“cheese”, “guacamole”, “sour cream”, “potato chips”, “jelly beans”]
  23. 35.
  24. 36.

    nil

  25. 37.
  26. 38.

    you already know what a method is. All that stuff

    we did to strings and arrays? Those were methods.
  27. 39.

    methods: return values def taco “tacos are delicious” end !

    puts taco # => “tacos are delicious”
  28. 41.

    more arguments def salutations(greeting, name) “#{greeting}, #{name.upcase}.” end ! puts

    salutations(“Goodbye”, “Angel”) # => “Goodbye, ANGEL.”
  29. 42.

    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.”
  30. 43.

    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”)
  31. 44.

    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.
  32. 47.