Ruby Goldへの道 day9
以下のサイトでruby gold取得に向けて毎日一回Goldチャレンジを行い間違えた問題を簡単にまとめる。 rex.libertyfish.co.jp
9日目 得点 72点/100点中
後2問。 いつも前から問題をみて気になった問題を選んでいるが今日は気分転換に後ろから。
require 'yaml' yaml = <<YAML sum: 510, orders: - 260 - 250 YAML # 文字列のYAMLデータをハッシュオブジェクトにしている object = YAML.load yaml # YAMLデータをrubyオブジェクトにしている p object #=> {"sum"=>510, "orders"=>[260, 250]}
- singlten_class
- Object.singleton_classで特異クラスを取得することができる
class C def self._singleton class << C self # レシーバのオブジェクトを返す # ここでは class C end end end p C._singleton #=> #<Class:C> class C end p C.singleton_class #=> #<Class:C> p C.new.singleton_class #=> #<Class:#<C:0x00007fcfc5823998>>
class Array def succ_each(step = 1) return enum_for(:succ_each, step) unless block_given? # 引数が2つ each do |int| yield int + step end end end p [98, 99, 100].succ_each(2).map {|succ_chr| succ_chr.chr} [101, 102, 103].succ_each(5) do |succ_chr| p succ_chr.chr end #=> ["d", "e", "f"] #=> "j" #=> "k" #=> "l" class Array def succ_each(step = 1) return to_enum(:succ_each) unless block_given? # 引数が2つ each do |int| yield int + step end end end p [98, 99, 100].succ_each(2).map {|succ_chr| succ_chr.chr} [101, 102, 103].succ_each(5) do |succ_chr| p succ_chr.chr end #=> ["c", "d", "e"] #=> "j" #=> "k" #=> "l" class Array def succ_each(step = 1) return to_enum(:succ_each, step) unless block_given? # 引数が2つ each do |int| yield int + step end end end p [98, 99, 100].succ_each(2).map {|succ_chr| succ_chr.chr} [101, 102, 103].succ_each(5) do |succ_chr| p succ_chr.chr end #=> ["d", "e", "f"] #=> "j" #=> "k" #=> "l"