On Aug 19, 2009, at 19:49, Derek S. wrote:
if table_exists == “”
p “yes”
else
p “no”
end
OUTPUT
$ ruby mail_log_miner.rb
“yes”
The variable “table_exists” is not nil and not false. It is probably
an empty array of rows, if you’re using the sqlite database adapter.
“”
The stringified version of the variable “table_exists” is an empty
string. This is what you would expect if “table_exists” is an empty
array:
irb(main):001:0> [].to_s
=> “”
Putting “#{foo}” inside a string basically says "call .to_s on the
object and concatenate the results with the beginning and end of this
string. In other words:
“#{table_exists}”
is the same as
“” + table_exists.to_s + “”
If you want to see a better representation of an object, just call “p
object” or “puts object.inspect”
“no”
No, "table_exists’ is not literally an empty string. You wouldn’t
expect that, however, since db.execute doesn’t return strings, but
arrays.
With a table that does exist using same if clauses.
ruby mail_log_miner.rb
“yes”
Yes, table_exists is not false and not nil.
“lines”
“#{table_exists}” is “lines”, i.e. table_exists.to_s is lines.
table_exists is probably [“lines”], i.e. an array with 1 element, the
string “lines”.
irb(main):002:0> [“lines”].to_s
=> “lines”
“no”
No, “table_exists” is not literally an empty string.
I think what you want is more along these lines:
Don’t call it “table_exists” because that implies the value is a
boolean, when in fact it will be an array
table_array = db.execute(“select name from sqlite_master where
type=‘table’ and tbl_name=‘derek’”)
if table_array.size > 0
p “yes”, table_array
end
if not table_array.empty?
p “yes”
else
p “no”
end
Ben