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

Introduction to Ruby (Rails Girls)

Introduction to Ruby (Rails Girls)

A short introduction to Ruby, created with one of the Rails Girls events in mind.

It tries to cover variables, simple calculations, conditions, functions, collections, symbols and classes.

3ea55d185aee5756c52056419238eec8?s=128

Lukasz Wrobel

May 25, 2014
Tweet

Transcript

  1. Introduction to Ruby Łukasz Wróbel

  2. About me • Architect, team leader; • high-traffic websites: ◦

    nk.pl; ◦ Gadu-Gadu. • “Memoirs of a Software Team Leader”; • @lukaszwrobel
  3. Agenda Ain't nobody got time for that! Pure essence.

  4. Computers are like guys Do this, then do that. Be

    precise.
  5. Say hello!

  6. puts "Hello, world!"

  7. Variables

  8. message = "Hello world!" puts message

  9. Calculations

  10. puts 5 + 3 * 2

  11. puts 5 + 3 * 2 # => 16?

  12. puts "H" + "ello"

  13. Conditions

  14. age = 22 if age > 30 puts "Too old"

    else puts "Acceptable" end
  15. Repetitions

  16. puts "Hello, John" puts "Hello, Mary" puts "Hello, everyone"

  17. Functions to the rescue!

  18. def say_hello(name) puts "Hello, " + name end

  19. say_hello("John") say_hello("Mary") say_hello("everyone")

  20. say_hello("John") say_hello("Mary") say_hello("everyone") # => "Hello, John" # => "Hello,

    Mary" # => "Hello, everyone"
  21. Introducing changes

  22. def say_hello(name) puts "Hello, " + name + "!" end

  23. Collections

  24. names = ["John", "Mary", "everyone"] names.each do |name| say_hello(name) end

  25. Plus a few nice features

  26. prices = [100, 9.99, 14] prices.reduce(:+)

  27. prices = [100, 9.99, 14] prices.reduce(:+) # => 123.99

  28. Filtering

  29. [9, 5, 3, 4, 7].find_all {|number| number > 5} #

    => [9, 7]
  30. Mapping

  31. ["These", "are", "words"].map {|word| word.upcase} # => ["THESE", "ARE", "WORDS"]

  32. Hashes

  33. friend = { "name" => "William", "age" => 45, }

    puts friend["name"] # => "William"
  34. Symbols Unique. More common.

  35. friend = { :name => "William", :age => 45, }

    puts friend[:name] # => "William"
  36. Classes

  37. class Person def say_hello(name) puts "My name is " +

    name end end somebody = Person.new somebody.say_hello("Emily")
  38. somebody = Person.new somebody.say_hello("Emily") somebody.say_hello("Somebody else") somebody.say_hello("Whatever")

  39. Not only methods, but also data

  40. class Person def initialize(name) @name = name end def say_hello

    puts "My name is " + @name end end me = Person.new("Łukasz") me.say_hello # => "My name is Łukasz"
  41. It’s just the beginning.

  42. Thank you! @lukaszwrobel