Skip to content

Instantly share code, notes, and snippets.

@zyphlar
Created March 21, 2013 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zyphlar/5217155 to your computer and use it in GitHub Desktop.
Save zyphlar/5217155 to your computer and use it in GitHub Desktop.
Input: ["en.subnets.show.&nbsp"]
Output: {"en"=>{"subnets"=>{"show"=>{"&nbsp"=>{}}}}}
def hashify(strings)
strings.map { |s| s.split('.') }.each_with_object({}) do |segmented_string, h|
segmented_string.each do |segment|
h[segment] ||= {}
h = h[segment]
end
end
end
Desired Output: {"en"=>{"subnets"=>{"show"=>{"&nbsp"=>"I AM THE LAST ELEMENT"}}}}
@zyphlar
Copy link
Author

zyphlar commented Mar 21, 2013

input = ["en.subnets.show.&nbsp.the end"]
hash = input.first.split('.').reverse.inject {|a, n| {n => a}}
=> {"en"=>{"subnets"=>{"show"=>{"&nbsp"=>"the end"}}}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment