Slide 1

Slide 1 text

ruby basics As much Ruby as Liz can teach you in >100 minutes.

Slide 2

Slide 2 text

hey girl hey! um, who are you? I am a very nice person who loves tacos.

Slide 3

Slide 3 text

workshop info puts ‘hello, beginners!’

Slide 4

Slide 4 text

code conventions

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

two ways to run ruby

Slide 9

Slide 9 text

irb your file $ irb $ puts “hello world” # => “hello world” $ exit ! ! # hello.rb puts “hello taco” ! $ ruby hello.rb # => “hello taco”

Slide 10

Slide 10 text

variables

Slide 11

Slide 11 text

variables a = 1 variable value

Slide 12

Slide 12 text

variable names YES ! hello_taco taco5000 ! NO! 99bottles 144324322 extra-chz taco’s?!
 WAT ? TACO Party

Slide 13

Slide 13 text

numbers

Slide 14

Slide 14 text

ruby can math! # hello.rb a = 1 b = 2 c = a + b puts a + b puts c $ ruby hello.rb # => 3 # => 3

Slide 15

Slide 15 text

what is d? a = 5 b = 10 c = 2 d = c * a - b what is d?

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

modulo # hello.rb puts 10 % 5 puts 10 % 3 ! $ ruby hello.rb # => 0 # => 1

Slide 18

Slide 18 text

strings

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

strings $ ruby hello.rb # => Liz Abinante # => Liz ‘The Taco’ Abinante

Slide 21

Slide 21 text

strings # hello.rb age = ‘28’ puts name + ‘ is ’ + age ! ! ! $ ruby hello.rb # => Liz Abinante is 28

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

playinG with strings (nicely)

Slide 24

Slide 24 text

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.

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

.reverse & .reverse! party = “all the time” party.reverse! # => “emit eht lla” puts party # => “emit eht lla”

Slide 28

Slide 28 text

<< letters = “abc” letters << “domino” puts letters # => “abcdomino”

Slide 29

Slide 29 text

arrays

Slide 30

Slide 30 text

arrays things_in_tacos = [“cheese”, “guacamole”] things_in_salsa = [“lime juice”, “mangos”] ! ! ! ! things_in_my_belly = [things_in_salsa, things_in_tacos, “ice cream”]

Slide 31

Slide 31 text

arrays: length array = [“chickens”, “two cents”, “ke$ha”] ! array.length # => 3

Slide 32

Slide 32 text

arrays: index array = [“chickens”, “two cents”, “ke$ha”] array[0] array[2] array[1]

Slide 33

Slide 33 text

arrays: new items taco = [“cheese”, “guacamole”] taco << “sour cream” taco << “potato chips” << “jelly beans” # => [“cheese”, “guacamole”, “sour cream”, “potato chips”, “jelly beans”]

Slide 34

Slide 34 text

playing with arrays (nicely... sometimes)

Slide 35

Slide 35 text

booleans

Slide 36

Slide 36 text

nil

Slide 37

Slide 37 text

methods

Slide 38

Slide 38 text

you already know what a method is. All that stuff we did to strings and arrays? Those were methods.

Slide 39

Slide 39 text

methods: return values def taco “tacos are delicious” end ! puts taco # => “tacos are delicious”

Slide 40

Slide 40 text

passing arguments def say_hi_to(name) “Hello #{name}!” end ! puts say_hi_to(“Buffy Summers”) # => “Hello Buffy Summers!”

Slide 41

Slide 41 text

more arguments def salutations(greeting, name) “#{greeting}, #{name.upcase}.” end ! puts salutations(“Goodbye”, “Angel”) # => “Goodbye, ANGEL.”

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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”)

Slide 44

Slide 44 text

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.

Slide 45

Slide 45 text

conditionals

Slide 46

Slide 46 text

everything is an object

Slide 47

Slide 47 text

classes