Xavier Noria
4/8/2007 9:16:00 PM
On Apr 8, 2007, at 10:40 PM, music wrote:
> Xavier Noria wrote:
>> On Apr 8, 2007, at 9:55 PM, music wrote:
>>> as you see prova@azienda.it is in hash and in arr then it is in
>>> @hash and in @a but @a.include?(value) it doesn't find it.
>> As I said before please inspect @a, its elements are not what you
>> think. As a hint
>> puts @a.first.class
>
> This returns Array, it seems the correct answer I think.
>
>> Then read the docs of String#scan to understand why you are after
>> something like
>> @a.flatten.compact
>
> Great.....with @a=riga1.scan(/<(.*?@.*?)>|\(\?\)/).flatten.compact
> in place of
> @a=riga1.scan(/<(.*?@.*?)>|\(\?\)/) puts @a.first.class returns
> String and the code now works well.
> Sorry but I'm newbie on ruby, can you explain why it works now?
When the regexp has groups String#scan returns an array of arrays,
each one consisting of the corresponding captures:
irb(main):002:0> "foo bar".scan(/\b(\S)|(\S)\b/)
=> [["f", nil], [nil, "o"], ["b", nil], [nil, "r"]]
Those nils come from the fact that even if the pipe guarantees only
one side will match, the regexp as such still has two groups.
When there are no groups you get an array of strings with the actual
matches
irb(main):003:0> "foo bar baz".scan(/\w+/)
=> ["foo", "bar", "baz"]
which I guess is what you thought @a contained.
-- fxn