2016年4月14日 星期四

『*』的用法


「*」的用法 

把 array 展開成豆號分隔的 參數


Ex1:
numbers = [1, 2, 3]
numbers_with_zero_and_100 = [0, *numbers, 100] # => [0, 1, 2, 3, 100]

 Ex2:

def get_year_month
  year_month ||= params[:year_month] || Date.today.strftime("%Y-%m")
  year_month.split(/-|\/|_/).map { |i| i.to_i }
end
Date.new(*(get_year_month)) # =>  Fri, 01 Apr 2016

沒有留言:

張貼留言