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
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 }
Hello,
On 23 Οκτ 2013, at 13:11 , Thiyagarajan V. [email protected] 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: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5