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

Ruby and Rails - Session 1

Ruby and Rails - Session 1

Ruby and Rails course for technical people that are not Ruby developers. First session, held @ Freeletics GmbH HQ

Monica Giambitto

November 08, 2017
Tweet

More Decks by Monica Giambitto

Other Decks in Programming

Transcript

  1. GET TO KNOW RUBY • 1993 • Japan • Matz

    (Matsumoto Yukihiro) • Perl, Smalltalk, Eiffel, Ada, Lisp • Scripting language • Goals: truly OO, simple syntax, GC, closures, portable • Multi paradigm (functional, OO, imperative) • Dynamically strongly typed • ~2000: Ruby 1.8 -> Duck Typing, Ruby on Rails, The Pickaxe Book • A favorite of Martin Fowler ;)
  2. “ I hope to see Ruby help every programmer in

    the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language. -Matz @ Google Tech Talk (2008)
  3. PHILOSOPHY • Designed to minimize work, confusion and to be

    enjoyable • Principles of good user interface design • Emphasize human needs over computer needs • MISWAN - Community
  4. “ People are part of the system. The design should

    match the user's experience, expectations, and mental models. -Saltzer and Kaashoek “Principles of computer system design: an introduction“ (2009)
  5. “ Everyone has an individual background. […] The principle of

    least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. -Matz in "The Philosophy of Ruby” by Venners (2015)
  6. RUBY IMPLEMENTATIONS • Reference implementation: MRI - Matz’s Ruby Interpreter

    a.k.a. CRuby alternatives
 • complete list https://github.com/cogitator/ruby- implementations/wiki/List-of-Ruby-implementations • most notable list https://www.ruby-lang.org/en/about/ in “Other Implementations of Ruby” • JRuby on top of JVM • Rubinius - Ruby in Ruby • mruby - embedded, led by Matz
  7. IRB REPL • Interactive Ruby Shell • Read - Eval

    - Print Loop • Command line tool with command history, line editing, network access
  8. BASIC SYNTAX - NAMING CONVENTIONS • Use English • snake_case

    for symbols, methods, variables • CamelCase for classes and modules (keep acronyms) • snake_case for file and directory names • Name of the file = name of the class -> One class, one file • SCREAMING_SNAKE_CASE for constants • Predicate methods end with a question mark, no prefix (ex. user.coach_subscription?) • Potentially dangerous methods end with an exclamation mark (a bang) • # comment • https://github.com/bbatsov/ruby-style-guide
  9. BASIC SYNTAX - DECLARATIONS • variable (local, instance, class, global)


    
 greeting = “hi”
 • method
 
 def say_hi
 puts “hi”
 end
  10. BASIC SYNTAX - CONTROL STRUCTURES unless condition something else something

    else end if !condition something else something else end
  11. BASIC SYNTAX - CONTROL STRUCTURES a = 2 case when

    a == 1, a == 2 puts "a is one or two" when a == 3 puts "a is three" else puts "I don't know what a is" end
  12. BASIC SYNTAX - LOOPS for i in 1..8 do puts

    i end 1.upto(5) do |i| puts i end 5.times do |i| puts i end
  13. BASIC SYNTAX - LOOPS array.each do |elem| puts elem end

    for elem in [1, 2, 3] do puts elem end
  14. BASIC SYNTAX - LOOPS i = 0 until i ==

    5 do puts i i += 1 end i = 0 while i < 5 do puts i i += 1 end
  15. DATA TYPES IN RUBY-CORE • Numeric: Integer, Float, Complex, Rational

    • Collections: Array, Hash, Range, Struct, Enumerator • Text: String, Symbol • Boolean: TrueClass, FalseClass, NilClass
  16. DATA TYPES - SYMBOLS :symbol :"yup still a symbol" "me

    too".to_sym Like a string, but instantiated only once per application execution
  17. DATA TYPES - ARRAYS Initialization [1, "two", 3] Array.new() #

    empty Array.new(quantity, content) %w[array of only words] Usage foo = [1, "two", 3] foo[1] # => "two"
  18. DATA TYPES - HASHES Initialization {one: "value", two: 20, three:

    [1, 2, 3]} {"one" => "value", "three" => [1, 2]} Hash.new() # empty Hash.new(default) # what to return if no key Usage foo = {first: "one", two: 2} foo[:first] # => "one" foo["first"] # => nil
  19. HANDS ON #1 • Anna is 38, she is from

    Germany, she prefers cats, earns 40k per year, she studied Economics • Mike is 27, he is from China, he prefers dogs, earns 40k per year, he studied Medicine • Svetla is 35, she is from Bulgaria, she prefers cats, earns 80k per year, she studied Electrical Engineering • Theo is 60, he is from Germany, he doesn’t like pets, earns 50k per year, he studied Law 1. Create appropriate data structures 2. Find all people who are from Europe 3. Find all people who earn between 45k and 70k per year 4. Find all people who don’t like pets
  20. CREDITS Monica Giambitto - 2017 This work is licensed under

    the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. I don't own the right to the images unless otherwise stated. If you feel you rights have been violated, please contact me at [email protected] To view a copy of this license visit 
 http://creativecommons.org/licenses/by-nc-sa/4.0/ You can find the slides on speakerdeck.com/nirnaeth