Guest User

Untitled

a guest
Mar 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. holiday_dec = {
  2.  
  3. :winter => {
  4. :christmas => ["Lights", "Wreath"],
  5. :new_years => ["Party Hats"]
  6. },
  7. :summer => {
  8. :fourth_of_july => ["Fireworks", "BBQ"]
  9. },
  10. :fall => {
  11. :thanksgiving => ["Turkey"]
  12. },
  13. :spring => {
  14. :memorial_day => ["BBQ"]
  15. }
  16.  
  17. }
  18.  
  19. def all_supplies_in_holidays(holiday_hash)
  20.  
  21. holiday_hash.each do |seasons, holidays|
  22.  
  23. holidays.each do |holidays, supplies|
  24. puts "#{seasons.to_s.capitalize}:"
  25. puts " #{holidays.to_s.tr("_"," ").capitalize}: #{supplies.join(", ")}"
  26. end
  27.  
  28. end
  29.  
  30. end
  31.  
  32. all_supplies_in_holidays(holiday_dec)
  33.  
  34. str.split('_').map(&:capitalize).join(' ')
  35.  
  36. "fourth_of_july".split('_') -> ["fourth", "of", "july"]
  37. ["fourth", "of", "july"].map(&:capitalize) -> ["Fourth", "Of", "July"]
  38. ["Fourth", "Of", "July"].join(' ') -> "Fourth Of July"
  39.  
  40. 'fourth_of_july'.titleize => "Fourth Of July"
  41.  
  42. holiday_dec.each_with_object({}) { |(k,v),h|
  43. h[k] = v.each_with_object({}) { |(kk,vv),g|
  44. g[kk.to_s.split('_').map { |s| s[0]=s[0].upcase; s }.join(' ')] = vv } }
  45. #=> {:winter=>{"Christmas"=>["Lights", "Wreath"], "New Years"=>["Party Hats"]},
  46. # :summer=>{"Fourth Of July"=>["Fireworks", "BBQ"]},
  47. # :fall=>{"Thanksgiving"=>["Turkey"]},
  48. # :spring=>{"Memorial Day"=>["BBQ"]}}
  49.  
  50. def key_changer hash
  51. hash.map do |k,v|
  52. [ k.to_s.scan(/[a-zA-Z]+/).map(&:capitalize).join(' '),
  53. v.class == Hash ? key_changer(v) : v ]
  54. end.to_h
  55. end
  56.  
  57. key_changer holiday_dec #=>
  58.  
  59. #{ "Winter" => { "Christmas" => ["Lights", "Wreath"],
  60. # "New Years" => ["Party Hats"] },
  61. # "Summer" => { "Fourth Of July" => ["Fireworks", "BBQ"] },
  62. # "Fall" => { "Thanksgiving" => ["Turkey"] },
  63. # "Spring" => { "Memorial Day" => ["BBQ"]}
  64. #}
  65.  
  66. irb> 'some_class_string'.classify
  67. => "SomeClassString"
Add Comment
Please, Sign In to add comment