Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- holiday_dec = {
- :winter => {
- :christmas => ["Lights", "Wreath"],
- :new_years => ["Party Hats"]
- },
- :summer => {
- :fourth_of_july => ["Fireworks", "BBQ"]
- },
- :fall => {
- :thanksgiving => ["Turkey"]
- },
- :spring => {
- :memorial_day => ["BBQ"]
- }
- }
- def all_supplies_in_holidays(holiday_hash)
- holiday_hash.each do |seasons, holidays|
- holidays.each do |holidays, supplies|
- puts "#{seasons.to_s.capitalize}:"
- puts " #{holidays.to_s.tr("_"," ").capitalize}: #{supplies.join(", ")}"
- end
- end
- end
- all_supplies_in_holidays(holiday_dec)
- str.split('_').map(&:capitalize).join(' ')
- "fourth_of_july".split('_') -> ["fourth", "of", "july"]
- ["fourth", "of", "july"].map(&:capitalize) -> ["Fourth", "Of", "July"]
- ["Fourth", "Of", "July"].join(' ') -> "Fourth Of July"
- 'fourth_of_july'.titleize => "Fourth Of July"
- holiday_dec.each_with_object({}) { |(k,v),h|
- h[k] = v.each_with_object({}) { |(kk,vv),g|
- g[kk.to_s.split('_').map { |s| s[0]=s[0].upcase; s }.join(' ')] = vv } }
- #=> {:winter=>{"Christmas"=>["Lights", "Wreath"], "New Years"=>["Party Hats"]},
- # :summer=>{"Fourth Of July"=>["Fireworks", "BBQ"]},
- # :fall=>{"Thanksgiving"=>["Turkey"]},
- # :spring=>{"Memorial Day"=>["BBQ"]}}
- def key_changer hash
- hash.map do |k,v|
- [ k.to_s.scan(/[a-zA-Z]+/).map(&:capitalize).join(' '),
- v.class == Hash ? key_changer(v) : v ]
- end.to_h
- end
- key_changer holiday_dec #=>
- #{ "Winter" => { "Christmas" => ["Lights", "Wreath"],
- # "New Years" => ["Party Hats"] },
- # "Summer" => { "Fourth Of July" => ["Fireworks", "BBQ"] },
- # "Fall" => { "Thanksgiving" => ["Turkey"] },
- # "Spring" => { "Memorial Day" => ["BBQ"]}
- #}
- irb> 'some_class_string'.classify
- => "SomeClassString"
Add Comment
Please, Sign In to add comment