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. RUBY & RAILS

    View full-size slide

  2. 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 ;)

    View full-size slide


  3. 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)

    View full-size slide

  4. PHILOSOPHY
    • Designed to minimize work, confusion and to be enjoyable

    • Principles of good user interface design

    • Emphasize human needs over computer needs

    • MISWAN - Community

    View full-size slide


  5. 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)

    View full-size slide

  6. POLA
    • Principle of least astonishment / surprise

    • Associated with Ruby, but!

    View full-size slide


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

    View full-size slide

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

    View full-size slide

  9. IRB REPL
    • Interactive Ruby Shell

    • Read - Eval - Print Loop

    • Command line tool with command history, line editing,
    network access

    View full-size slide

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

    View full-size slide

  11. BASIC SYNTAX - DECLARATIONS
    • variable (local, instance, class, global)


    greeting = “hi”

    • method


    def say_hi

    puts “hi”

    end

    View full-size slide

  12. BASIC SYNTAX - CONTROL STRUCTURES
    if condition1
    something
    elsif condition2
    something else
    else
    finally
    end

    View full-size slide

  13. BASIC SYNTAX - CONTROL STRUCTURES
    if condition
    something
    end

    View full-size slide

  14. BASIC SYNTAX - CONTROL STRUCTURES
    something if condition

    View full-size slide

  15. BASIC SYNTAX - CONTROL STRUCTURES
    unless condition
    something
    else
    something else
    end
    if !condition
    something
    else
    something else
    end

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. BASIC SYNTAX - LOOPS
    array.each do |elem|
    puts elem
    end
    for elem in [1, 2, 3] do
    puts elem
    end

    View full-size slide

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

    View full-size slide

  20. BASIC SYNTAX - LOOPS
    puts i while i < 5
    puts i until i == 5

    View full-size slide

  21. DATA TYPES IN RUBY-CORE
    • Numeric: Integer, Float, Complex, Rational

    • Collections: Array, Hash, Range, Struct, Enumerator

    • Text: String, Symbol

    • Boolean: TrueClass, FalseClass, NilClass

    View full-size slide

  22. DATA TYPES - STRINGS
    "I am a string"
    'I am also a string'

    View full-size slide

  23. DATA TYPES - SYMBOLS
    :symbol
    :"yup still a symbol"
    "me too".to_sym
    Like a string, but instantiated only once per application execution

    View full-size slide

  24. DATA TYPES - SYMBOLS
    "test".object_id
    => 70308044363980
    "test".object_id
    => 70308044357840
    :test.object_id
    => 196968
    :test.object_id
    => 196968

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  29. THANKS!
    @KFMOLLI
    GITHUB.COM/NIRNAETH
    SPEAKERDECK.COM/NIRNAETH

    View full-size slide