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. ruby basics As much Ruby as Liz can teach you

    in >100 minutes.
  2. hey girl hey! um, who are you? I am a

    very nice person who loves tacos.
  3. workshop info puts ‘hello, beginners!’

  4. code conventions

  5. text editor is pink # taco.rb taco = “yummy” puts

    taco
  6. terminal is green $ ruby taco.rb # => “yummy” !

    $ ruby --v
  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.
  8. two ways to run ruby

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

    => “hello world” $ exit ! ! # hello.rb puts “hello taco” ! $ ruby hello.rb # => “hello taco”
  10. variables

  11. variables a = 1 variable value

  12. variable names YES ! hello_taco taco5000 ! NO! 99bottles 144324322

    extra-chz taco’s?!
 WAT ? TACO Party
  13. numbers

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

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

    = 2 d = c * a - b what is d?
  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
  17. modulo # hello.rb puts 10 % 5 puts 10 %

    3 ! $ ruby hello.rb # => 0 # => 1
  18. strings

  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
  20. strings $ ruby hello.rb # => Liz Abinante # =>

    Liz ‘The Taco’ Abinante
  21. strings # hello.rb age = ‘28’ puts name + ‘

    is ’ + age ! ! ! $ ruby hello.rb # => Liz Abinante is 28
  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.
  23. playinG with strings (nicely)

  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.
  25. .delete & .delete! me = “Liz” me.delete(“z”) # => “Li”

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

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

    => “emit eht lla” puts party # => “emit eht lla”
  28. << letters = “abc” letters << “domino” puts letters #

    => “abcdomino”
  29. arrays

  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”]
  31. arrays: length array = [“chickens”, “two cents”, “ke$ha”] ! array.length

    # => 3
  32. arrays: index array = [“chickens”, “two cents”, “ke$ha”] array[0] array[2]

    array[1]
  33. arrays: new items taco = [“cheese”, “guacamole”] taco << “sour

    cream” taco << “potato chips” << “jelly beans” # => [“cheese”, “guacamole”, “sour cream”, “potato chips”, “jelly beans”]
  34. playing with arrays (nicely... sometimes)

  35. booleans

  36. nil

  37. methods

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

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

    puts taco # => “tacos are delicious”
  40. passing arguments def say_hi_to(name) “Hello #{name}!” end ! puts say_hi_to(“Buffy

    Summers”) # => “Hello Buffy Summers!”
  41. more arguments def salutations(greeting, name) “#{greeting}, #{name.upcase}.” end ! puts

    salutations(“Goodbye”, “Angel”) # => “Goodbye, ANGEL.”
  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.”
  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”)
  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.
  45. conditionals

  46. everything is an object

  47. classes