File.open('file.txt', 'r').each do |c|
if(c=="__#{t.month}/#{t.mday}/#{t.year} \(#{t.zone}\)")
e=true
end
end
E.g. of date in file
__6/26/2008 (Pacific Daylight Time)
I'm trying to see if the date already exists in file.txt. Since it
already does, it must set e=true, but the if(c== ...) statement doesn't
seem to evaluate to true, so e never becomes true! =(
# I'm trying to see if the date already exists in file.txt. Since it
# already does, it must set e=true, but the if(c== ...)
# statement doesn't seem to evaluate to true, so e never becomes true!
if(c=="__#{t.month}/#{t.mday}/#{t.year} \(#{t.zone}\)")
e=true
end
end
E.g. of date in file
__6/26/2008 (Pacific Daylight Time)
I'm trying to see if the date already exists in file.txt. Since it
already does, it must set e=true, but the if(c== ...) statement doesn't
seem to evaluate to true, so e never becomes true! =(
#
# File.open('ManageLists_Changelog.txt', 'r').each do |c|
# if(c=="__#{t.month}/#{t.mday}/#{t.year} \(#{t.zone}\)")
# puts "TESTING"
# e=true
# end
# end
hmmm, if e is already true, why would i let it go through that code wc just sets it to true again??
maybe, you wanted test e first like,
if not e
File.open('ManageLists_Changelog.txt', 'r').each do |c|
if c=="__#{t.month}/#{t.mday}/#{t.year} \(#{t.zone}\)"
e=true
end
end
end
(note, i'm just following the logic of your code)
now, when comparing strings, string#size usually catches the diff. and when debugging/printing values, it would be better if you use #p instead of #puts since #p quotes the whole string expression and displays invisible characters too (by escaping them w a backslash on display)
eg,
p "asdf\n\t"
=>"asdf\n\t"
vs.
puts "asdf\n\t"
=>asdf
print "asdf\n\t"
asdf
#=> nil
ergo, i would then do my test loop like,
File.open('ManageLists_Changelog.txt', 'r').each do |c|
d="__#{t.month}/#{t.mday}/#{t.year} \(#{t.zone}\)"
p c
p d
p c == d
end
Instead of c.chomp! as it was suggested, use c=c.chomp(" ") before the
"if"statement. The problem seems to be a white space at the end of
"c".. if you use print instead of puts you'll see it.
About your second problem I just tested it and works fine for me.
Maybe txt file encoding is wrong...be sure to save it with utf-8 enc.
···
On Jun 27, 6:21 pm, Justin To <te...@hotmail.com> wrote:
I have another problem also:
File.open("genver.txt", 'r').each do |x|
puts x
end
The file contains:
v4
But 'puts x' outputs ÿþv Nul4 Nul
--
Posted viahttp://www.ruby-forum.com/.
On Jun 27, 6:21�pm, Justin To <te...@hotmail.com> wrote:
--
Posted viahttp://www.ruby-forum.com/.
Hello,
Instead of c.chomp! as it was suggested, use c=c.chomp(" ") before the
"if"statement. The problem seems to be a white space at the end of
"c".. if you use print instead of puts you'll see it.
About your second problem I just tested it and works fine for me.
Maybe txt file encoding is wrong...be sure to save it with utf-8 enc.