Ruby check if variable exists in a file -
i have text file of 1 word per line.
i want check if ruby variable exists on lines in file.
@username = john
and in bannedwords.txt:
alex adam mary john james
so true. best function use in ruby this?
just read in file , convert set
can use test inclusion:
require 'set' banned = set.new(file.readlines('bannedwords.txt').collect(&:chomp)) banned.include?('john') # => true banned.include?('papa smurf') # => false
Comments
Post a Comment