Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering –...

86
Testautomatisering Ruby Repetition

Transcript of Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering –...

Page 1: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Testautomatisering Ruby Repetition

Page 2: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Idag

FM: ◦ Kort utvärdering Lab 1

◦ Ruby Repetition

Page 3: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Denna veckan

Ruby Repetition

Unit Test Intro

Bra Unit Test, TDD, Videoföreläsning

Page 4: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Utvärdering Lab 1

◦ 1. Uppskatta hur mycket tid du lagt på lab 1 (h)

◦ 2. Uppskatta kvarvarande tid för lab 1 (h)

◦ 3. Svårighet: För Lätt / Lagom / För Svår

◦ 4. Lärdommar - “Jag ser... “: Ingen praktisk nytta (-)

Viss praktisk nytta (0)

Stor praktisk nytta (+)

Page 5: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ngt om språkbruk

Jag hör hemma i C#

Jag kan en stor rad språk

Begreppen är ibland olika i olika språk

Jag slarvar en del med uttrycken

Ex: metod/funktion ◦ Metod är en funktion som hör hemma i en klass

Vi oklarheter: Fråga

Page 6: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Syfte ◦ Vi skriver Unit Tests i Ruby

Ex: Klass ”TestUser”

◦ Lab 2 kommer innebära Implementation skriven i Ruby Unit Tester skriven i Ruby

◦ Unit Tester innebär inte så mycket nytt Men kräver förståelse för grunderna i språket

◦ Sammantaget behöver vi veta något om ruby för att kunna genomföra detta

Page 7: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Road Map ◦ Hur lång tid tar det att bli en bra programmerare?

◦ Vilka steg bör man ta?

Page 8: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Road Map

◦ 1. Codecademy eller Learn Ruby The Hard Way

typ: “tutorial” eller intro-bok scope: timmar till dagar

Page 9: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Road Map

◦ 2a. Annan bok - helst “Uppslagsverk” ex: Ruby Programming 1.9

◦ 2b. Börja koda enkla exempel - använd google + språkets API

typ: mer uttömmande bok eller API scope: dagar till veckor

Page 10: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Road Map

◦ Nu kan du något om språket - plocka på ramverk för det du är intresserad av

Ex: Rails - Ramverk för att skapa webbapplikationer med Ruby

Eller: test/unit – för att skapa enhetstester i ruby

Page 11: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Road Map

◦ 3. Nettuts om Ruby on Rails

typ: tutorial

scope: timmar till dagar

Page 12: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Road Map

◦ 4. Sätt dig in i ramverket

typ: mer uttömmande

scope: dagar till veckor

◦ anmärkning: De flesta genrella ramverk av denna typ är mer omfattande än språket i sig

Page 13: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4. Programmering – Experiment/Djupare bok för ramverket

=> 5. Skapa webbapplikationer 6. Best Practices (fokus på detta först här)

Shortcuts: En van programmerare kan göra steg 1-4 på några timmar

◦ => kanske ok kod

En programmerare som inte är van kan hoppa över saker och genomföra 1-4 på några timmar ◦ => dålig kod/dålig förståelse

Page 14: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Hur lång tid tar det att bli en “bra” programmerare? ◦ snävt område, ex. variabler: veckor

◦ bredare område, ex. skapa en blog i RoR: månader

◦ generellt område, ex. skapa webbapplikationer i Ruby on

Rails: år

◦ mer generellt område, ex. skapa webbapplikationer: år++

Snabbt svar: 5-10 effektiva år, aktiv inom flera

områden/språk

Page 15: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Låt inte detta skrämma er

◦ ”Anställningsbar” utvecklare: görbart på 6 mån?

◦ Gissningsvis har ni redan nu insikter som en ”normal” nybörjare inte har.

◦ Ni kommer ha nytta av detta långt, långt innan ni är ”bra”

Page 16: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Hur lång tid tar det att bli en “bra” programmerare? ◦ Intelligens / Talang har väldigt lite med detta att göra (många vill dock påskina detta)

◦ Nyfikenhet, Envishet och Tid är nödvändigt…

◦ Om du ”vet” hur du lär dig bäst så kan du korta ned tiden ordentligt

Page 17: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

En första varning ◦ Jag kommer inte fokusera särskilt mycket på vad som är korrekt/bra programmering

◦ (Däremot kommer jag prata om vad som utgör ett bra enhetstest)

Page 18: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

En första varning

◦ Ni kommer höra saker som använd inte “for”-loopen, använd “each”

◦ Detta har ett syfte, men är inte särskilt behjälpligt för nybörjare

Page 19: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

En första varning ◦ ”Använd inte for-loopen”

◦ Syftet är att en viss kodbas skall vara konsistent

◦ Syftet är också att vana programmerare skall “veta” vad de förväntas använda

◦ Inget av ovanstående är särskilt viktigt när du lär dig programmera

Page 20: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Som nybörjare bör ni prioritera dessa frågor: ◦ 1. Funkar det? (inga felmeddelanden)

◦ 2. Är det korrekt? (rätt output)

◦ 3. Vet du varför det funkar?

Om inte: skriv mer kod

◦ (4. Finns det bättre sätt att göra det på?)

Page 21: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Varför bör vi inte prioritera:

(4. Finns det bättre sätt att göra det på?) ◦

◦ Vad är bäst (som i effektivast/snabbast)

För det mesta: Det beror på

För det mesta: Det spelar ingen roll

◦ Vad är bäst (som i vad kommer vara enklast att förstå/underhålla?)

Om ni måste veta: resurser om några slides

Page 22: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Varför bör vi inte prioritera: (4. Finns det bättre sätt att göra det på?)

◦ Lär oss gå innan vi springer

◦ Jag kommer dock nämna något om detta i

förbifarten under lektionerna

◦ Jag kommer säkerligen tipsa om saker som hör hit under handledningen

◦ Ni får gärna fråga

Page 23: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

”Bra kod”: ◦ OBS!!! Se till att ni är hyfsat trygga med språket ifråga, ex. Ruby, innan ni funderar över detta.

◦ Ni bör ta en första titt på detta hyfsat tidigt, men inte direkt (~Några veckor/månader in)

◦ Återvänd till detta kontinuerligt

Page 24: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

”Bra kod”: ◦ Resurser - För Ruby: Ruby Style Guide (Nätresurs) - För Ruby: Ruby Best Practices (Nätresurs) - Allmänt: Clean Code (Bok - Java) - Allmänt: Code Complete (Bok - C/Java)

Page 25: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

”Bra kod” i korthet ◦ Lätt att underhålla

Lätt att förstå

Modulär

◦ Vi kontrollerar våra antaganden

Page 26: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Och nu: Ruby

Page 27: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om klasser ◦ En klass är en samling data och beteende som hör ihop

◦ Data: Variabler i klassen

◦ Beteende: Metoder för klassen

Page 28: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om klasser ◦ Generellt sett ett abstrakt koncept

◦ Ett objekt är en konkret instans av en klass

◦ Ex:

◦ Vi kan prata om ”Bil” som ett abstrakt koncept

◦ Bilen i mitt garage är en konkret instans av klassen ”Bil”

Page 29: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om klasser ◦ Exempel:

class Car

◦ def start puts ”Starting the engine…”

◦ end

End

concrete_car = Car.new concrete_car.start

Page 30: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om klasser ◦ Kodexempel

Page 31: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om klasser ◦ Vi återkommer till detta senare idag

Page 32: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler har: ◦ Namn – ex: ”my_num”

◦ Värde – ex: 3

◦ Scope – ex: endast tillgänglig i aktuell metod

◦ Exempel

my_num = 3

Page 33: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler – namn ◦ Liten bokstav + bokstäver + siffror + ’_’

◦ Vissa ord får inte användas som variabelnamn ex:: if, elsif, def, class, ..

google “ruby reserved words”

◦ Bör vara så beskrivande som möjligt

◦ Exempel ◦ “car_to_be_sold”, “current_user”, etc…

Page 34: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler – namn ◦ Kodexempel

Page 35: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Man kan se variabler som en behållare som innehåller ett värde och har ett namn

◦ Ex:

my_int = 3

Page 36: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Värdet har en typ

Bool, Integer, Float, String, Nil

◦ Ex: my_int = 3

my_bool = false

my_float = 3.14

◦ Ta reda på typ: puts my_var.class

my_var.is_a? Integer

Page 37: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Typen för värdet blir intressant då vi blandar värden av olika typ

◦ Ex:

puts ”Balance: ” + balance + ” USD” # Ger fel

puts ”Balance: ” + balance.to_s + ” USD”

Dock:

puts ”Balance: #{balance} USD”

Page 38: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Integer

◦ Float

Page 39: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Bool

true, false

Boolska operatorer: ◦ && || !

Jämförande operatorer: ◦ == != <= >= < >

Används för programflöde (t.ex. if) / för att hålla status

Page 40: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Strängar

Text ◦ Ex: ”Jag är en sträng”

skillnad på ‘#{}’ och “#{}” - även “what’s that?”

Metoder på strängar: ◦ count, +

Fler exempel på strängmetoder:

http://www.ruby-doc.org/core-1.9.3/String.html

Page 41: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ symboler Lättviktssträngar

Använd för fasta värden – ex. :name

:name != ”name”

Mer om symbol:

http://www.ruby-doc.org/core-1.9.3/Symbol.html

Page 42: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler - Värde ◦ Klasser

◦ Vi kan även tilldela instanser av en klass (objekt) till variabler.

Page 43: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler – Något om tilldelning (assignment)

◦ += …: my_var = my var + …

◦ -= …: my_var = my_var - …

◦ etc…

◦ ||= …: my_var = my_var || … # (default om nil)

Page 44: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler – olika scope ◦ Variabler är inte tillgängliga överallt after vi definierat dem

◦ var de är tillgängliga beror på deras scope

Page 45: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler – olika scope ◦ local variable: local_variable

tillgänglig

◦ instance variable: @instance_variable tillgänglig i ett objekt

◦ class variable: @@class_variable tillgänglig för alla objekt i en klass

◦ global variable: $global_variable tillgänglig överallt i samma projekt

◦ Finns fler: (, constant)

Page 46: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Variabler… ◦ Kan vara ytterliggare typer – ex. lambda, proc, etc.

Page 48: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Array ◦ En lista med flera element

◦ [”first”, ”second”, ”third”]

Page 49: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Array ◦ En lista med flera element

◦ Väldigt vanliga

En lista över inloggade användare

En lista över bloggposter på en sida

Etc, etc…

Page 50: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Array ◦ En lista med flera element

◦ Skapa my_array = [”First”, ”Second”]

Alt: Array.new

◦ Tilldelning my_array[0] = ”Test”

◦ Plocka ut värde puts my_array[0]

Page 51: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Array ◦ Metoder – exempel:

◦ clear, collect, count, delete, each, empty?, include?, push, pop, sort, sort!

◦ Kan vi lista ut vad ovanstående gör mha länken på nästa sida?

Page 52: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Array ◦ Var hittar jag metoder för arrayer?

◦ Array API:

http://www.ruby-doc.org/core-1.9.3/Array.html

Page 53: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

hash (dictionary) ◦ En uppsättning key/value-par

Varje key är associerat med ett visst värde

Varje key är unik

◦ Väldigt vanliga

Se det som en “lättvikts”-klass

Används ofta för att föra över data mellan olika moduler i ett system

Page 54: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

hash ◦ Skapa my_hash = {”key1” => ”val1”, ”key2” => ”val2”}

my_hash = {”key1”, ”val1”, ”key2”, ”val2”}

my_hash = Hash.new

◦ Lägg till värde my_hash[”name”] = ”David”

◦ Ta ut värde puts my_hash[”name”]

Page 55: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

hash ◦ Exempel på metoder

◦ keys, values, clear, delete, include?, size

◦ Kan vi lista ut vad dessa metoder innebär med hjälp av länken på nästa sida?

Page 57: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – villkorat flöde (Conditional)

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_if_else.htm

◦ -if, elsif, else

kodexempel

Page 58: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – villkorat flöde (Conditional)

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_if_else.htm

◦ -unless

kodexempel

Page 59: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – villkorat flöde (Conditional)

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_if_else.htm

◦ -case when then

kodexempel

Page 60: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – villkorat flöde (Conditional)

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_if_else.htm

◦ (ternary operator - ? : )

kodexempel

Page 61: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – Loop

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_loops.htm

◦ - while do + break, next (, retry, redo)

kodexempel

Page 62: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – Loop

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_loops.htm

◦ - until do

kodexempel

Page 63: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – Loop

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_loops.htm

◦ - for … in …

kodexempel

Page 64: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Flöde – Loop

Snabb-guide: http://www.tutorialspoint.com/ruby/ruby_loops.htm

◦ (- loop)

kodexempel

Page 65: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Metoder

Varför? ◦ Gör koden mer lättläst

◦ Återanvänder kod

Page 66: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Metoder

Snabbguide: http://www.tutorialspoint.com/ruby/ruby_methods.htm

◦ Definition

Kodexempel

Page 67: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Metoder

Snabbguide: http://www.tutorialspoint.com/ruby/ruby_methods.htm

◦ Argument / parametrar

◦ Default parametrar

Kodexempel

Page 68: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Metoder

Snabbguide: http://www.tutorialspoint.com/ruby/ruby_methods.htm

◦ Return

◦ Att kalla på en metod

Kodexempel

Page 69: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Jag tar ej upp Lambda, Proc

Detta är kraftfulla byggstenar, men vi klarar oss utan dem.

När är det dags att lära sig lambda? ◦ Du har stött på det några gånger när du letat

lösningar på problem (via ex. google), och blivit imponerad av hur mycket enklare lösningen blir med lambda-uttryck

De är definitivt värda att lära sig dock…

Page 70: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser ◦ Varför?

◦ Knyter samman data och beteende för en viss entitet

Ex: alla kod som rör användare i vårt system finns i klassen ”User”

◦ Gör koden mer lättläst (abstraktioner)

◦ Modulariserar kod (enklare att underhålla)

◦ Döljer data

◦ (Arv, polymorfism, etc…)

Page 71: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser ◦ Snabbguide:

http://www.tutorialspoint.com/ruby/ruby_classes.htm

◦ Definition

◦ initialize – new

Page 72: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser ◦ Snabbguide:

http://www.tutorialspoint.com/ruby/ruby_classes.htm

◦ Metoder

Page 73: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser ◦ Snabbguide:

http://www.tutorialspoint.com/ruby/ruby_classes.htm

◦ instance variabler

◦ attr_reader

◦ attr_writer

◦ attr_accessor

Page 74: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser ◦ Snabbguide:

http://www.tutorialspoint.com/ruby/ruby_classes.htm

◦ public/private

Page 75: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser ◦ Snabbguide:

http://www.tutorialspoint.com/ruby/ruby_classes.htm

◦ klassvariabler (används inte särskillt ofta)

◦ klassmetoder (self)

Page 76: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Klasser – om arv: ◦ vi kommer använda oss av detta då vi utnyttjar vissa ramverk (t.ex. testramverk).

◦ Vi klarar oss dock långt utan att själva använda arv

◦ Exempel: class InheritedClass < BaseClass

Page 77: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Exceptions ◦ Något oväntat har inträffat -> exekvering avbryts

Page 78: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Exceptions

Kasta med ”raise”

Hantera med begin, rescue, ensure, end

Page 79: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Exceptions

Exempel: begin

raise 'A test exception.'

rescue Exception => e

puts e.message puts

ensure

puts "Ensuring execution"

end

Page 80: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om att debugga ◦ Se först till att du kan reproducera buggen

◦ print / puts - kontrollera dina antaganden

◦ gå steg för steg

◦ utnyttja irb (el. skapa ett minimalt exempel)

◦ Steg 2: Unit Test

◦ Resurs: Kap 6 i ruby best practices (går även djupare in på detta)

Page 81: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Snabbt om att debugga ◦ Ej i denna kurs: debugger

Page 83: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Andra resurser

Thomas, Fowler, Hunt: Programming Ruby 1.9

◦ När ni kommit in något i språket

Flanagan, Matsumoto: The Ruby Programming Language

◦ Alternativ till ovanstående

Ruby Best Practices (Hela boken) ◦ Efter någon av ovanstående böcker – eller när ni programmerat ruby

ett tag

Page 84: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Ruby Repetition

Andra resurser

Om ni väljer en annan bok:

◦ Ruby 1.8 släpptes 2003

◦ Ruby 1.9 släpptes 2007

◦ Ruby 1.9.3 släpptes 2011

◦ Ruby 2.0 släpptes igår

◦ Se till att boken ni väljer gäller v.1.9 (bra 2.0 böcker dröjjer nog lite)

Page 85: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Nästa gång

Unit Test – Intro

Lab 1

Page 86: Ruby Repetition - WordPress.comRuby Repetition 1. Tutorial för språket 2. Programmering – Experiment/Djupare bok för språket 3. Tutorial för ramverk (ex. webbutveckling) 4.

Fin