0

Hey, I am trying to convert this object to an array of ints e.g. array(3,4,6)

- - !ruby/object:Code 
attributes: 
  candidate_id: "3"
attributes_cache: {}

- !ruby/object:Code 
attributes: 
  candidate_id: "4"
attributes_cache: {}

- !ruby/object:Code 
attributes: 
  candidate_id: "6"
attributes_cache: {}

here is what I tried

result = @intersection.map{|c| c.candidate_id}

Thanks

5
  • Your code should work. What is the error you are getting? Commented Jul 20, 2010 at 16:09
  • I get - undefined method `candidate_id' for [#<Code >, #<Code >, #<Code >]:Array Commented Jul 20, 2010 at 16:13
  • Can you give us the result of @intersection.inspect , please? I suspect it is an array of arrays of Code objects, actually. Commented Jul 20, 2010 at 18:39
  • Thanks for the reply! --- "[[#<Code >, #<Code >, #<Code >]]" Commented Jul 20, 2010 at 18:45
  • Looks like it's an array of array. Try @intersection.first.map... Commented Jul 20, 2010 at 20:25

1 Answer 1

1

Try the following:

  • c[:candidate_id]

OR

  • c[:attributes][:candidate_id]

I'm not familiar with YAML, however you might want to try making hashes instead of Code objects. Additionally, if you do use a code class, you should ensure that you have and attr_accessor (and then your old code should work).

Sign up to request clarification or add additional context in comments.

3 Comments

I get a - undefined method `attributes' for [#<Code >, #<Code >, #<Code >]:Array
using c[:candidate_id] with a attr_accesser I get a type error (Symbol as array index)
you do c.candidate_id with attr_accessor

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.