What did I wrong here

Hello,

I have this array of hashes :

data = [
{age: 40, sex: :m},
{age: 24, sex: :f},
{age: 56, sex: :m},
{age: 45, sex: :m},
{age: 23, sex: :f}
]

Now I try to find all the people which sex is female so sex: f
I did try this :

data.select{ |data2| data2[:sex] == “:f”}

But I do not get any output.

Where did I go wrong here ?

Roelof

:f is a string not symbol. what you want is:

data.select{ |data2| data2[:sex] == :f }

-- Juanito Fatas
Juanito Fatas Huang schreef op 4-6-2014 8:56:

:f is a string not symbol. what you want is:

data.select{ 
|data2| data2[:sex] == :f }
???




?? ?? --??Juanito Fatas


On Wed, Jun 4, 2014 at 2:51 PM, Roelof Wobben <[email protected]> wrote:
Hello,

I have this array of hashes :

data = [
?? {age: 40, sex: :m},
?? {age: 24, sex: :f},
?? {age: 56, sex: :m},
?? {age: 45, sex: :m},
?? {age: 23, sex: :f}
]

Now I try to find all the people which sex is female so sex: f
I did try this :

data.select{ |data2| data2[:sex] == ":f"}

But I do not get any output.

Where did I go wrong here ?

Roelof


Thanks,

I noticed one things. The output shows the arrays but not in a array as I expected.

Roelof

On Wed, Jun 4, 2014 at 12:39 PM, Roelof W. [email protected] wrote:

I noticed one things. The output shows the arrays but not in a array as I
expected.

What do you mean by that? Output is as expected:

irb(main):008:0> data.select{ |data2| data2[:sex] == :f }
=> [{:age=>24, :sex=>:f}, {:age=>23, :sex=>:f}]

Cheers

robert

Roelof W. wrote in post #1148790:

Robert K. schreef op 4-6-2014 12:58:

robert

Wierd, when I do it on
http://www.compileonline.com/execute_ruby_online.php
I see this output :

{:age=>24, :sex=>:f} {:age=>23, :sex=>:f} so no array it seems.

Roelof

You are doing something wrong, as Array#select returns an array.
Tried myself on the linked page and

print data.select{ |data2| data2[:sex] == :f }

results in

[{:age=>24, :sex=>:f}, {:age=>23, :sex=>:f}]

Robert K. schreef op 4-6-2014 12:58:

robert

Wierd, when I do it on
http://www.compileonline.com/execute_ruby_online.php
I see this output :

{:age=>24, :sex=>:f} {:age=>23, :sex=>:f} so no array it seems.

Roelof

Sorry to interrupt. Could you please refrain from sending HTML-only E-
mails? It messes up at least the forum view, and, to be honest, I don’t
display HTML code in e-mails in any case. So all I get is the markup,
and I
immediately loose any interest in reading and answering your question.

Thanks
Eric

On Wednesday 04 June 2014, 12:39:43, Roelof W. wrote:

Juanito Fatas Huang schreef op 4-6-2014 8:56:

:f is a string not symbol. what
you
want is: :sex] == :f } <div

title="MDH:YDpmYCBpcyBhIHN0cmluZyBub3Qgc3ltYm9sLiB3aGF0IHlvdSB3YW
50IGlzOjxka

XY+PGJyPjwvZGl2PjxkaXY+YGBgcnVieTwvZGl2PjxkaXY+PHNwYW4gc3R5bGU9I
mNvbG9yOiByZ

2IoMCwgMCwg

MCk7IGZvbnQtZmFtaWx5OiBhcmlhbCwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAx
M3B4OyI+ZGF

0

YS5zZWxlY3R7IHxkYXRhMnwgZGF0YTJbOnNleF0gPT0gOmYgfTwvc3Bhbj48YnI+
PC9kaXY+PGR

p

dj48c3BhbiBzdHlsZT0iY29sb3I6IHJnYigwLCAwLCAwKTsgZm9udC1mYW1pbHk6I
GFyaWFsLCB

z
YW5zLXNlcmlmOyBmb250LXNpemU6IDEzcHg7Ij5gYGA8L3NwYW4+PC9kaXY+"
style=“height:0;font-size:0em;padding:0;margin:0”>​

<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font- </blockquote> <p>size:13px"></span></p> <blockquote> <p></font></div></p> <div><font face="&aps;courier new&aps;, monospace"

color="#666666">

On Wednesday 04 June 2014, 12:58:49, Robert K. wrote:

What do you mean by that? Output is as expected:

irb(main):008:0> data.select{ |data2| data2[:sex] == :f }
=> [{:age=>24, :sex=>:f}, {:age=>23, :sex=>:f}]

Cheers

robert

Juanito has spotted the mistake: The OP wrote data2[:sex] == ":f", thus comparing the Symbol to a String.

  --- Eric