0

I have an Hash @answers = params[:Answers].to_hash;

<%= debug @answers %> outs

--- 
"1": "2"
"7": "3"
"6": "4"
"4": "0"

Need to make the @answers to one like below

@ans = {1 => 2, 7 => 3, 6 => 4, 4 => 0} <%= debug @answers %> outs

--- 
1: 2
7: 3
6: 4
4: 0
2
  • Why do you want that ? It's a debug so it's like you do p no puts Commented Dec 21, 2010 at 10:16
  • P: debug, in a Rails view, formats its argument into yaml. Commented Dec 21, 2010 at 10:30

2 Answers 2

11

Black magic wizard reporting in:

answers = {"1" => "2", "3" => "4"}
Hash[*answers.to_a.flatten.map(&:to_i)] # => {1=>2, 3=>4}
Sign up to request clarification or add additional context in comments.

1 Comment

double tested on ruby 1.8.7 and 1.9.2 . Make sure that "*" is present and answers is not empty
3

Here is the code

@ans = {"1" => "2", "7" => "3", "6" => "4", "4" => "0"}
@foo_hash ={} #new_hash
@ans.each_pair{|k,v| @foo_hash.store(k.to_i,v.to_i)}

The @foo_hash will be {1 => 2, 7 => 3, 6 => 4, 4 => 0}

Then you can <%= debug @foo_hash %> to get the output in your YAML format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.