Forum: Ruby need to help on to use delete_if

Bff863fb0ac32943895f336f87392262?d=identicon&s=25 Thiyagarajan V. (thiyagarajan_v)
on 2013-10-23 13:11
I need a help to fix the below code,Quick help is highly appriciated

Assume :

new_entry = TOM
nodelist = John, Tom, Ram

I have return the below code in ruby to remove the "TOM" from the "node
list" variable


def filter_word_in (new_entry, nodelist)
nodelist.delete_if {|new_entry| !new_entry.match(nodelist)}
return nodelist
end

Expected output:

node  => John, Ram


Regards
Thiyagarajan
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-23 13:19
This is fundamentally flawed:
nodelist.delete_if {|new_entry| !new_entry.match(nodelist)}
In this, you're ignoring the method-local variable new_entry by adding a
block-local variable with the same name.

Assuming these are strings, and standardised (TOM does not equal Tom):

new_entry = 'Tom'
nodelist = 'John', 'Tom', 'Ram'

nodelist.delete( new_entry )

Or you could use this:

nodelist.delete_if { |val| val == new_entry }
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2013-10-23 13:32
(Received via mailing list)
Hello,

On 23 Οκτ 2013, at 13:11 , Thiyagarajan V. <lists@ruby-forum.com> wrote:

>
> def filter_word_in (new_entry, nodelist)
> nodelist.delete_if {|new_entry| !new_entry.match(nodelist)}
> return nodelist
> end
>
> Expected output:
>
> node  => John, Ram

Here

https://gist.github.com/atmosx/7116937

>
>
> Regards
> Thiyagarajan
>
> --
> Posted via http://www.ruby-forum.com/.

I'm not sure if this is an exercise of something, but what's important
to note here is that you're doing everything in RAM. When having a list
it's better to work with database, YAML or any XML format would be a
good choice for similar sort of data.

Panagiotis (atmosx) Atmatzidis

email:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.