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. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. “ Instance variable live in objects; methods live in classes

    -Paolo Perrotta in “Metaprogramming Ruby”
  7. 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
  8. RUBY OBJECT MODEL - METHOD LOOKUP obj ———- @var =

    10 obj.my_method() MySubclass class MyClass ———- my_method() Object superclass superclass BasicObject superclass Ruby
  9. 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
  10. 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
  11. 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