Ruby
# 使用 Array 類別
a = Array.new
# 使用中括號
b = []
# %w 幫你建立出「字串」的陣列
list = %w(ruby php python)
p list # 印出 ["ruby", "php", "python"]
heroes = ['孫悟空', '魯夫', '宇智波佐助', '一拳超人', '流川楓', '黑崎一護', '劍心'];
puts heroes[-1] # 印出 劍心
puts heroes[-2] # 印出 黑崎一護
puts heroes.first # 印出 孫悟空
puts heroes.last # 印出 劍心
puts heroes.second # ruby沒有,但rails有
puts heroes.length
heroes << '漩渦鳴人' # 在最後面加一個人
heroes.push('布羅利')
對集合裡的每個元素進行運算,並收集成一個新的集合。
同意詞:collect
p (1..10).map { |x| x * 2 }
# 印出 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
從集合裡挑選符合條件的元素,並收集成一個新的集合。
同意詞:filter
反意詞:reject
p (1..10).select { |x| x < 5 }
# 印出 [1, 2, 3, 4]
p (1..10).select { |x| x < 5 }
# 印出 [5, 6, 7, 8, 9, 10]
對集合裡的每個元素進行運算,並將所有的運算結果歸納成一個單一結果
同意詞:inject
p (1..10).reduce { |sum, n| sum + n }
# 印出 55
# 把陣列 [1, 2, 3, 4, 5] 變成 [1, 3, 5, 7, 9]
list = [1, 2, 3, 4, 5]
p list.map { |x| x + (x-1) }
p list.map { |x| x * 2 - 1}
# 把陣列 [1, 3, 4, 1, 7, nil, 7] 由小到大排序,並且移除nil 以及重複的數字
list = [1, 3, 4, 1, 7, nil, 7]
p list.compact.sort.uniq
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