Ruby
def say_hello_to(someone)
puts "Hello, #{someone}"
end
say_hello_to('孫悟空') # 印出 Hello, 孫悟空
say_hello_to('魯夫') # 印出 Hello, 魯夫
一個蘿蔔一個坑
def say_hello_to(someone)
puts "Hello #{someone}"
end
def greeting
puts "你好"
end
say_hello_to # 發生引數個數錯誤(少一個)
greeting '孫悟空' # 發生引數個數錯誤(多一個)
say_hello_to('孫悟空')
say_hello_to '孫悟空'
age = 18
def age
return 20
end
puts age # 18
puts age() # 20
def say_hello_to(someone = '一拳超人')
puts "Hello #{someone}"
end
say_hello_to '孫悟空' # 印出 Hello 孫悟空
say_hello_to # 印出 Hello 一拳超人
link_to
方法有幾個參數?在 Rails 的 View 裡常看到這樣的寫法(產生連結):
link_to "⾸⾴", root_path, class:"btn btn-default", method: "post", confirm: true
link_to ("⾸⾴", root_path, {class:"btn btn-default", method: "post", confirm: true})
如果最後一個參數是一個 Hash 的話,那個 Hash 的大括號可省略。
回傳 = 交回控制權
def doubleA(num)
p num * 2 # 印出 2 倍的值,但沒有回傳值
end
def doubleB(num)
return num * 2 # 回傳 2 倍的值,但不會印出內容
end
doubleA(5) # 呼叫 doubleA 方法
p doubleB(5) # 印出呼叫 doubleB 方法的內容
def add_three(n)
n + 3 # 回傳 +3 之後的結果
end
def idle
end
def say_hello
puts "你好" # puts 本身無回傳值
end
p add_three(2) # 印出 5
p idle # 會印出什麼? nil
p say_hello # 會印出什麼? nil
def is_adult?(age)
age >= 18
end
puts is_adult?(20)
list = [9, 5, 2, 7]
p list.sort # 進行排序,但不會改變 list 的內容
p list
p list.sort! # 進行排序,並且直接改變 list 的內容
p list
Familiarity with Rakuten Travel QA Workflow (Hands-On Experience)
Dec 4, 2024A compilation of essential vocabulary gathered during my internship at Rakuten, with a sum of 94.
Mar 21, 2024在 Ruby 裡,幾乎什麼東西都是物件
Mar 1, 2024create web applications in Ruby
Mar 1, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up