7. van PHP naar Ruby
(overeenkomsten)
Geen variabelen declareren
Er zijn classes met public, protected en private toegang
String interpolatie php: “$foo is een $bar” ruby: “#{foo} is een #{bar}”
Er zijn Exceptions
true en false hebben dezelfde behaviour als in php
8. van PHP naar Ruby
(verschillen)
Ruby kent strong typing: je moet to_s, to_i, etc ... gebruiken bij
conversies
strings, nummers, arrays, hashes zijn objecten abs(-1) wordt -1.abs
gebruik van () is optioneel bij functies
variabelen zijn referenties
Er zijn geen abstracte Classes
Hashes en Arrays zijn niet omwisselbaar met elkaar
Alleen false en nil zijn false: 0, array(), “” zijn true
22. Attributen
Getters
class BookInStock
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
def isbn
@isbn
end
def price
@price
end
# ..
end
23. Attributen
Getters Setters
class BookInStock
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
def isbn
@isbn
end
def price
@price
end
# ..
end
24. Attributen
Getters Setters
class BookInStock class BookInStock
def initialize(isbn, price) def initialize(isbn, price)
@isbn = isbn @isbn = isbn
@price = Float(price) @price = Float(price)
end end
def isbn def price=(new_price)
@isbn @price = new_price
end end
def price
@price # ..
end end
# ..
end
25. The Ruby Way
class BookInStock
attr_reader :isbn
attr_accessor :price
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
# ...
end
26. The Ruby Way
class BookInStock
attr_reader :isbn
attr_accessor :price
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
# ...
end
book = BookInStock.new("isbn1", 33.80)
puts "ISBN = #{book.isbn}"
=> ISBN = isbn1
27. The Ruby Way
class BookInStock
attr_reader :isbn
attr_accessor :price
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
# ...
end
book = BookInStock.new("isbn1", 33.80)
puts "ISBN = #{book.isbn}"
=> ISBN = isbn1
puts "Price = #{book.price}"
=> Price = 33.8
28. The Ruby Way
class BookInStock
attr_reader :isbn
attr_accessor :price
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
# ...
end
book = BookInStock.new("isbn1", 33.80)
puts "ISBN = #{book.isbn}"
=> ISBN = isbn1
puts "Price = #{book.price}"
=> Price = 33.8
book.price = book.price * 0.75
puts "New price = #{book.price}"
=> New price = 25.349999999999998
34. Hashes
Een collectie van objecten, dat geïndexeerd kan
worden door elk objecttype
h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' }
35. Hashes
Een collectie van objecten, dat geïndexeerd kan
worden door elk objecttype
h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' }
h = { :dog => 'canine', :cat => 'feline', :donkey => 'asinine' }
36. Hashes
Een collectie van objecten, dat geïndexeerd kan
worden door elk objecttype
h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine' }
h = { :dog => 'canine', :cat => 'feline', :donkey => 'asinine' }
37. Symbolen
Unieke constanten zonder waarden
Eerste karakter “:” gevolgd door een naam
Voornamelijk gebruikt als keys voor Hash
{:name => “Ruby”} of {name: “Ruby”}
40. Blocks en Iterators
top_five = {“de” => 1, “kat” => 1, “zat” => 1, “op” => 1, “de” => 1, “mat” => 1}
for i in 0...5
word = top_five[i][0]
count = top_five[i][1]
puts "#{word}: #{count}"
end
41. Blocks en Iterators
top_five = {“de” => 1, “kat” => 1, “zat” => 1, “op” => 1, “de” => 1, “mat” => 1}
for i in 0...5
word = top_five[i][0]
count = top_five[i][1]
puts "#{word}: #{count}"
end
top_five.each do |word, count|
puts "#{word}: #{count}"
end
42. Blocks en Iterators
top_five = {“de” => 1, “kat” => 1, “zat” => 1, “op” => 1, “de” => 1, “mat” => 1}
for i in 0...5
word = top_five[i][0]
count = top_five[i][1]
puts "#{word}: #{count}"
end
top_five.each do |word, count|
puts "#{word}: #{count}" Block
end
43. Blocks en Iterators
top_five = {“de” => 1, “kat” => 1, “zat” => 1, “op” => 1, “de” => 1, “mat” => 1}
for i in 0...5
word = top_five[i][0]
count = top_five[i][1]
puts "#{word}: #{count}"
end
top_five.each do |word, count| Iterator
puts "#{word}: #{count}" Block
end
44. Blocks
Een stuk code tussen {} of do end keywords
Kan parameters meekrijgen tussen | param1 |
Parameters zijn altijd gescoped binnen het
block
Variabelen zijn gescoped binnen een block,
behalve als ze buiten het block gedefinieerd
staan
45. Blocks
sum = 0
[1, 2, 3, 4].each do |value|
square = value * value
sum += square
end
puts sum
=> 30
46. Blocks
sum = 0 square = Shape.new(sides: 4)
[1, 2, 3, 4].each do |value| #
square = value * value # .. een heleboel code
sum += square #
end
sum = 0
puts sum [1, 2, 3, 4].each do |value|
=> 30 square = value * value
sum += square
end
puts sum
square.draw # BOOM!
47. Blocks
sum = 0 square = Shape.new(sides: 4)
[1, 2, 3, 4].each do |value| #
square = value * value # .. een heleboel code
sum += square #
end
sum = 0
puts sum [1, 2, 3, 4].each do |value; square|
=> 30 square = value * value
sum += square
end
puts sum
square.draw
49. Inheritance
class Parent
end
class Child < Parent
end
puts "The superclass of Child is #{Child.superclass}"
=> “The superclass of Child is Parent”
puts "The superclass of Parent is #{Parent.superclass}"
=> “The superclass of Parent is Object”
puts "The superclass of Object is #{Object.superclass}"
=> “The superclass of Object is BasicObject”
puts "The superclass of BasicObject is #{BasicObject.superclass.inspect}"
=> “The superclass of BasicObject is nil”
51. Modules
module Trig
PI = 3.141592654
def Trig.sin(x)
puts x
end
end
module Moral
VERY_BAD = 0
BAD = 1
def Moral.sin(badness)
puts badness
end
end
52. Modules
module Trig
PI = 3.141592654
def Trig.sin(x)
puts x
end
end
module Moral
VERY_BAD = 0
BAD = 1
def Moral.sin(badness)
puts badness
end
end
y = Trig.sin(Trig::PI/4)
wrongdoing = Moral.sin(Moral::VERY_BAD)
=> 0.7853981635
=> 0
53. Modules
module Trig
PI = 3.141592654
def Trig.sin(x)
puts x
end Module methods
end
module Moral
VERY_BAD = 0
BAD = 1
def Moral.sin(badness)
puts badness
end
end
y = Trig.sin(Trig::PI/4)
wrongdoing = Moral.sin(Moral::VERY_BAD)
=> 0.7853981635
=> 0
58. Mixins
module Debug
def who_am_i?
"#{self.class.name} (id: #{self.object_id}): #{self.name}"
end
end
59. Mixins
module Debug
def who_am_i?
"#{self.class.name} (id: #{self.object_id}): #{self.name}"
end
end
class Phonograph class EightTrack
include Debug include Debug
attr_reader :name attr_reader :name
def initialize(name) def initialize(name)
@name = name @name = name
end end
# ... # ...
end end
60. Mixins
module Debug
def who_am_i?
"#{self.class.name} (id: #{self.object_id}): #{self.name}"
end
end
class Phonograph class EightTrack
include Debug include Debug
attr_reader :name attr_reader :name
def initialize(name) def initialize(name)
@name = name @name = name
end end
# ... # ...
end end
61. Mixins
module Debug
def who_am_i?
"#{self.class.name} (id: #{self.object_id}): #{self.name}"
end
end
class Phonograph class EightTrack
include Debug include Debug
attr_reader :name attr_reader :name
def initialize(name) def initialize(name)
@name = name @name = name
end end
# ... # ...
end end
ph = Phonograph.new("West End Blues")
et = EightTrack.new("Surrealistic Pillow")
ph.who_am_i?
et.who_am_i?
62. Mixins
module Debug
def who_am_i?
"#{self.class.name} (id: #{self.object_id}): #{self.name}"
end
end
class Phonograph class EightTrack
include Debug include Debug
attr_reader :name attr_reader :name
def initialize(name) def initialize(name)
@name = name @name = name
end end
# ... # ...
end end
ph = Phonograph.new("West End Blues")
et = EightTrack.new("Surrealistic Pillow")
ph.who_am_i? # => "Phonograph (id: 2151894340): West End Blues"
et.who_am_i? # => "EightTrack (id: 2151894300): Surrealistic Pillow"
63. Include
include is alleen een referentie naar de
module
Als de module in een ander bestand staat
dan is require nodig om de module
beschikbaar te maken
LET OP: Als een method wijzigt in de module
dan wijzigt deze voor alle classes die de
module includen
65. Methods
Worden definieerd met def
Beginnen met een kleine letter of een
underscore (_)
Kunnen eindigen met een letter of cijfer
Kunnen eindigen met een ? (boolean)
Kunnen eindigen met een = (setters)
Kunnen eindigen met een ! (bang methods)
73. Expresions
[ 3, 1, 7, 0 ].sort.reverse # => [7, 3, 1, 0]
song_type = if song.mp3_type == MP3::Jazz
if song.written < Date.new(1935, 1, 1)
Song::TradJazz
else
Song::Jazz
end
else
Song::Other
end
rating = case votes_cast
when 0...10 then Rating::SkipThisOne
when 10...50 then Rating::CouldDoBetter
else Rating::Rave
end
74. Vergelijken van objecten
== Test dezelfde waarde.
=== Vergelijk of een element binnen een bepaald berijk valt. (1..10) === 5
<=> Algemene vergelijkingsoperator. Geeft -1 bij kleiner dan, 0 bij gelijk, of +1 bij groter dan
<,<=,>=,> Vergelijking voor kleiner dan, kleiner en gelijk, groter dan, groter en gelijk.
=~ Reguliere expressie vergelijking
eql? True als de elementen dezelfde waarde en type hebben 1.eql?(1.0) is false.
equal? True als de elementen dezelfde objectID hebben