Slide 1

Slide 1 text

RUBY & RAILS

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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