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

Ruby and Rails - Session 2

Ruby and Rails - Session 2

Monica Giambitto

November 21, 2017
Tweet

More Decks by Monica Giambitto

Other Decks in Programming

Transcript

  1. RUBY & RAILS

    View Slide

  2. 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 Slide

  3. HANDS ON #1 - SOLUTIONS
    https://repl.it/@nirnaeth/technicalhandson1arrays
    https://repl.it/@nirnaeth/technicalhandson1hashes

    View Slide

  4. CLASSES
    class Name
    # some code describing the class behavior
    end
    Name.new

    View Slide

  5. CLASS AND INSTANCE METHODS
    class Computer
    # instance m.
    def model
    "MacBook Pro 2015"
    end
    # class m.
    def self.brand
    "apple"
    end
    end
    laptop = Computer.new
    laptop.model
    Computer.brand

    View Slide

  6. CONSTRUCTOR
    class Computer
    def initialize(user_name)
    @user_name = user_name
    end
    end
    WAIT WAT

    View Slide

  7. LOCAL AND INSTANCE VARIABLES
    class Computer
    def initialize(user_name)
    @user_name = user_name
    end
    def greet_user
    greet = "Hello"
    "#{greet} User #{@user_name}"
    # "Hello " + @user_name
    end
    end

    View Slide

  8. ACCESSORS
    my_computer = Computer.new("Monica")
    => #
    my_computer.user_name
    NoMethodError: undefined method `user_name' for #0x00007f9d8981a910 @user_name="Monica">

    View Slide

  9. ACCESSORS
    class Computer
    attr_accessor :user_name
    def initialize(user_name)
    @user_name = user_name
    end
    end
    def user_name=(str)
    @name = str
    end
    def user_name
    @name
    end

    View Slide

  10. ACCESSORS
    attr_accessor -> getter & setter
    attr_reader -> getter
    attr_writer -> setter

    View Slide

  11. METHODS VISIBILITY
    • visibility modifiers are methods defined on the Module
    class

    • public by default

    • protected is barely used: any instance of a class can call
    a protected method on any other instance of the class

    • private is only callable from inside the class or
    subclasses -> won’t allow calls with an explicit receiver

    View Slide

  12. CODE EXAMPLE
    https://repl.it/@nirnaeth/technicalvisibility

    View Slide


  13. Instance variable live in objects;
    methods live in classes
    -Paolo Perrotta
    in “Metaprogramming Ruby”

    View Slide

  14. RUBY OBJECT MODEL
    When you call a method, Ruby does two things:

    1. it finds the method (method lookup)

    2. it executes the method on something called self

    View Slide

  15. RUBY OBJECT MODEL - METHOD LOOKUP
    obj
    ———-
    @var = 10
    obj.my_method()
    MySubclass
    class
    MyClass
    ———-
    my_method()
    Object
    superclass
    superclass
    BasicObject
    superclass Ruby

    View Slide

  16. RUBY OBJECT MODEL - SELF AWARENESS
    • every line of Ruby code is executed inside an object called
    current object, symbolized by self

    • reference to the receiver that ruby holds when traversing
    the class hierarchy

    • only one object at a time can be self
    • when you call a method, the receiver becomes self
    • everything is an object -> in a class or a module, the role
    of self is take by the class or the module itself

    View Slide

  17. HANDS ON #2
    • Model a To Do List application

    • The app has an owner and zero or more lists

    • A list has an id, a name (mandatory) and zero or more items

    • An item has an id, a content (mandatory) and an expire date (not
    mandatory)

    • It should be possible to display all the expired items of a specific list

    • It should be possible to display all items of a list

    • It should be possible to display all lists

    • When an item has an expire date, that should also be displayed
    together with the name when listing the items

    • Lists can be added to and deleted from the app

    • Items can be added to and deleted from a list

    • Divide classes appropriately, make use of as many constructs we saw
    today

    View Slide

  18. 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 Slide

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

    View Slide