0

I am completely new to Ruby. I have this data in variable '@member'

 [{"clicks"=>"1", "member"=>{"email"=>"[email protected]", "id"=>"f6d02be6", "euid"=>"f02be6", "email_type"=>"html", "ip_signup"=>nil, "leid"=>"3223865", "clients"=>[], "static_segments"=>[], "notes"=>[]}}, {"clicks"=>"1", "member"=>{"email"=>"[email protected]", "id"=>"336ed115c4", "euid"=>"3365c4", "email_type"=>"html", "ip_signup"=>nil, "timestamp_signup"=>nil, "ip_opt"=>nil, "leid"=>"14877", "clients"=>[], "static_segments"=>[], "notes"=>[]}}, {"clicks"=>"2", "member"=>{"email"=>"[email protected]", "id"=>"334a7f", "euid"=>"334617", "email_type"=>"html", "ip_signup"=>nil, "leid"=>"377", "clients"=>[], "static_segments"=>[], "notes"=>[]}}, {"clicks"=>"1", "member"=>{"email"=>"[email protected]", "id"=>"4e9b7", "euid"=>"4e9b7", "email_type"=>"html", "ip_signup"=>nil, "timestamp_signup"=>nil, "ip_opt"=>nil, "leid"=>"4605", "clients"=>[], "static_segments"=>[], "notes"=>[]}}, {"clicks"=>"1", "member"=>{"email"=>"[email protected]", "id"=>"083fd3", "euid"=>"083fd3", "email_type"=>"html", "ip_signup"=>nil, "timestamp_signup"=>nil, "ip_opt"=>nil, "leid"=>"314649", "clients"=>[], "static_segments"=>[], "notes"=>[] }}] 

I need to get member emails, euid and leid from above data into array of struct/hashes.

I tried doing

@emails = @member.map { |x| x['member']['email']['euid']['leid'] }

But it returns nil.

Can anyone help in how to do it?

Please forgive if it is too basic, but m not able to solve it.

4 Answers 4

4

To get the fields as an array:

@member.map { |m| m["member"].values_at('email','euid','leid') }
#=> [["[email protected]", "f02be6", "3223865"], ["[email protected]", "3365c4", "14877"], ["[email protected]", "334617", "377"], ["[email protected]", "4e9b7", "4605"], ["[email protected]", "083fd3", "314649"]]

Or as a hash:

@member.map { |m| m["member"].select { |k, v| ['email','euid','leid'].include? k } }
#=> [{"email"=>"[email protected]", "euid"=>"f02be6", "leid"=>"3223865"}, {"email"=>"[email protected]", "euid"=>"3365c4", "leid"=>"14877"}, {"email"=>"[email protected]", "euid"=>"334617", "leid"=>"377"}, {"email"=>"[email protected]", "euid"=>"4e9b7", "leid"=>"4605"}, {"email"=>"[email protected]", "euid"=>"083fd3", "leid"=>"314649"}]

BTW, you should name that variable @members (plural).

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

Comments

0
@data = @member.map{|x| {'email' => x['member']['email'], 'euid' => x['member']['euid'], 'leid' => x['member']['leid']} }

Comments

0

You should use like this and get it to your array

@emails = @member.map { |x| x['member']['email'], x['member']['euid'], x['member']['leid'] }

because x['member']['email']['euid'] is not same as x['member']['euid']

Comments

0

Now that your problem has been answered, I would like to offer some advice on posting questions to SO. Normally comments are used for advice, but that would not work in this case because of formatting limitations. Here's one way you might have posed your question:


I have the following array:

@members = [ \
  {"clicks"=>"1", \
    "member"=>{"email"=>"[email protected]", "euid"=>"f02be6", "leid"=>"3"}}, \
  {"clicks"=>"1", \
    "member"=>{"email"=>"[email protected]", "ip_signup"=>nil, "leid"=>"14877"}}, \
  {"clicks"=>"2", \
    "member"=>{"email"=>"[email protected]", "euid"=>"334617", "leid"=>"377"}}]

I want to extract only the hash values for keys email and leid, and put them into an array, like this:

member_arr = [["[email protected]", "3"], ["[email protected]", "14877"], \
    ["[email protected]", "leid"=>"377"]]

I am new to Ruby. How can I do this?


In rephrasing the question, I have done the following

  • reduced the numbers of keys of interest from 3 to 2, and eliminated most of the hash elements, to simplify the example without simplifying the problem.
  • used the line continuation character (\) so that @members can be seen without horizontal scrolling and also to clarify the structure of @members.
  • was precise in explaining what I wanted to achieve and showed the desired output for the example input.

One other thing. You selected your preferred answer about one hour after posting the question, possibly as soon as reading an answer that worked for you. By waiting longer--at least a few hours--one often gets better and/or more varied answers. You can see when an answer was selected by placing your mouse cursor on the checkmark. Have a look at that for other SO questions.

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.