E
Erik Veenstra
What's wrong with the code below?
It returns this "f" instead of "". Where does "f" come from?
Thanks.
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
$ ruby test.rb
:bar
"f" <<<< Should be ""
nil
$ echo "select * from key_values;" | sqlite3 test.db
1|--- :foo
|--- :bar
2|--- :empty
|f
3|--- :nil
|
----------------------------------------------------------------
require "rubygems"
require "active_record"
class KeyValue < ActiveRecord::Base
serialize :key
serialize :value
def self.create_table
unless connection.tables.include?(table_name)
connection.create_table(table_name) do |table|
table.column :id , rimary_key
table.column :key , :text
table.column :value , :text
end
end
end
def self.[](key)
find_or_create_by_key(key).value
end
def self.[]=(key, value)
config = find_or_create_by_key(key)
config.value = value
config.save
end
end
File.delete("test.db") if File.file?("test.db")
ActiveRecord::Base.establish_connectionadapter=>"sqlite3", :database=>"test.db")
KeyValue.create_table
KeyValue[:foo] = :bar
KeyValue[:empty] = ""
KeyValue[:nil] = nil
p KeyValue[:foo]
p KeyValue[:empty]
p KeyValue[:nil]
----------------------------------------------------------------
It returns this "f" instead of "". Where does "f" come from?
Thanks.
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
$ ruby test.rb
:bar
"f" <<<< Should be ""
nil
$ echo "select * from key_values;" | sqlite3 test.db
1|--- :foo
|--- :bar
2|--- :empty
|f
3|--- :nil
|
----------------------------------------------------------------
require "rubygems"
require "active_record"
class KeyValue < ActiveRecord::Base
serialize :key
serialize :value
def self.create_table
unless connection.tables.include?(table_name)
connection.create_table(table_name) do |table|
table.column :id , rimary_key
table.column :key , :text
table.column :value , :text
end
end
end
def self.[](key)
find_or_create_by_key(key).value
end
def self.[]=(key, value)
config = find_or_create_by_key(key)
config.value = value
config.save
end
end
File.delete("test.db") if File.file?("test.db")
ActiveRecord::Base.establish_connectionadapter=>"sqlite3", :database=>"test.db")
KeyValue.create_table
KeyValue[:foo] = :bar
KeyValue[:empty] = ""
KeyValue[:nil] = nil
p KeyValue[:foo]
p KeyValue[:empty]
p KeyValue[:nil]
----------------------------------------------------------------