Exercises

把問號換成合適的方法,結合 split、shuffle 和 join 實作一個函數,把字串中的字母順序打亂。

>> def string_shuffle(s)
>>   s.?('').?.?
>> end
>> string_shuffle("foobar")
=> "oobfra"

參考下列程式碼,把 shuffle 方法加到 String Class 中。

>> class String
>>   def shuffle
>>     self.?('').?.?
>>   end
>> end
>> "foobar".shuffle
=> "borafo"

建立三個 Hash,分別命名為 person1、person2 和 person3,把名和姓派值給 :first 和 :last 鍵。然後建立一個名為 params 的 Hash,讓 params[:father] 對應 person1,params[:mother] 對應 person2,params[:child] 對應 person3。驗證一下 params[:father][:first] 的值是否正確。


上網搜尋 Ruby API,瞭解 Hash 的 merge 方法的用法。下面這個運算式的結果是什麼?

{ "a" => 100, "b" => 200 }.merge({ "b" => 300 })