Ruby on Rails 網站程式設計基礎班
LESSON 3
作業一
- 解答已公佈
小考一
- 請寫成 .md 檔
- push 上 github
更正
- case... when
operation = 3
case operation
# 記住 when 後面不要加 判斷式,只需寫比較的結果即可
when 1 then puts "operation is equal to 1"
when 2 then puts "operation is equal to 2"
when 3 then puts "operation is equal to 3"
when 4 then puts "operation is equal to 4"
end
unless
-- 文法上可以想成 "除非"
var = false
# 寫在符合條件要執行的程式碼後方
puts "It's true" unless var
更簡單的 if
# 可以把 if...end 精簡成一行,與其這樣寫:
operation = true
if operation == true
puts "operation is true"
end
# 可以改成這樣:
puts "operation is true" if operation == true
# if 是可以放在判斷式後方的,同時 end 也可審略掉
# 但是這個寫法只有在判斷式和要執行的程式法很簡單的狀況下適用
Conditional Assignment
favorite_book = nil
puts favorite_book
#如果 favorite book 還沒有被設值,那就把它設定成左邊的值
favorite_book ||= "麥田捕手"
puts favorite_book
favorite_book ||= "1984"
puts favorite_book
Implicit Return
# 在 ruby 的 method 裡,其實在大部份的情況下不需要 return 關鍵字
def greater_than_five(operation)
if operation >= 5
true
else
false
end
end
operation = 5
puts greater_than_five(operation)
#印出 true
def area_of_circle(radius)
radius_squared = radius ** 2
area = radius_squared * 3.14
# 重點是,ruby 會回傳最後一行被計算的結果
end
puts area_of_circle(10)
# 印出 314.0
Object Oriented
- 物件導向
- 程式設計的思維
- 封裝資料和方法到物件裡
- 封裝後的程式碼可被重複使用
Object
- Ruby語言裡,任何東西都是物件!
- 程式設計的思維
- 封裝資料和方法到物件裡
- 封裝後的程式碼可被重複使用
Class
- 類別
- 可以想像成樣板
# 宣告一個叫 Person 的 class
# 注意 class 名稱要用 CamelCase,也就是每一個字的第一字母要大寫
class RubyEngineer
# 任何 class 一定要加建構式
def initialize(name) # 建構式
@name = name # 物件變數
end
def greet_me(word)
puts "#{word}, #{@name}"
end
end
# 用 new 創出兩個 person 的物件
eugene = RubyEngineer.new("Eugene")
overlord = RubyEngineer.new("Overlord88")
# person 物件才能使用 greet_me() 方法
eugene.greet_me("Good Morning")
overlord.greet_me("Hello")
#印出 Good Morning, Eugene
#印出 Hello, Overlord88
initialize
- 每新創一個物件就會被呼叫
- 正式名稱是 constructor (建構式)
class Person
# 每一個 person 物件在創出時會被呼叫到
# 正式名稱是 constructor (建構式)
def initialize(name)
puts "This object was initialized!"
end
end
bob = Person.new
# => "This object was initialized!"
instance variable
- 實例變數
- 綁定資料在 object 上
# instance variable(實例變數) 是把資料綁定在 object 上的方法
# 只要被創出來的 object 還存在,instance variable 紀錄的資料也存在
class Person
def initialize(name, age)
# instance variable 前面一定要加 @
@name = name
@age = age
end
end
instance method
# instance method(實例方法) 是只有被創造出來的物件才可使用
class Person
def initialize(name, age)
# instance variable 等同於在這個 class 的區域變數
@name = name
@age = age
end
# 宣告 instance methods,就像宣告一般的 method,只不過在 class 裡面
def greet
# instance variable 只可在 class 裡面被使用
puts "Hello, my name is #{@name}"
end
end
bob = Person.new("bob", 17)
bob.greet
# => Hello, my name is bob
# 若沒有先創出 Person 的instance (實例),也就是先 new 一個 Person 的 object
# 使用 instance method 就會噴錯
Person.greet
# => instance_method.rb:22:in `<main>':
# undefined method `greet' for Person:Class (NoMethodError)
Accessor Methods I
# 假設我只想印出 bob 的歲數,我需要讀取 @name
class Person
def initialize(name, age)
@name = name
@age = age
end
def greet
puts "Hello, my name is #{@name}"
end
# 定義 get_name
def get_name
@name
end
end
bob = Person.new("bob", 17)
puts bob.get_name
# => bob
Accessor Methods II
# 假設我是要寫入或更改 @name
# 不能直接使用 bob.name = "Bob"
class Person
def initialize(name, age)
@name = name
@age = age
end
def greet
puts "Hello, my name is #{@name}"
end
# 讀取資料方法,稱為 getter method
def get_name
@name
end
# 寫入/更改方法,稱為 setter method
def set_name=(name)
@name = name
end
end
bob = Person.new("bob", 17)
puts bob.get_name
# => bob
bob.set_name = "Bob"
puts bob.get_name
# => Bob
attr_accessor
# 當然每次寫 getter 和 setter method 很麻煩,所以聰明的 ruby 就有了 attr_accessor 方法
class Person
# 在 attr_accessor 關鍵字後方加上 instance method 的 symbol
# 自動幫使用者把 getter 和 setter method 寫好
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
bob = Person.new("bob", 17)
puts bob.name
puts bob.age
# => bob
# => 17
bob.name = "Bob"
bob.age = 30
puts bob.name
puts bob.age
# => Bob
# => 30
課堂練習
ntu_ror_261_lesson3
By Eugene Chang
ntu_ror_261_lesson3
- 902