From: Justin Collins Date: 2009-01-07T15:11:53+09:00 Subject: Re: noob question: object to string Patrick Doyle wrote: > On Mon, Jan 5, 2009 at 11:02 AM, Darin Ginther < > darin.ginther@allwebleads.com> wrote: > > >> So I'm able to exercise creating my own class and returning an object >> from that class. I'm having trouble treating the return as an integer >> (for the purpose of a < or > compare).. Yet another noob syntax >> question. >> >> >> compare.rb: >> myinitialdbcount = Dbutil.new(mydb, dbuser, dbpw, counttable, >> leadtype) >> puts "Initial Count:" >> initialcount = myinitialdbcount.count_table{ |data| p data } >> >> This returns: >> Initial Count: >> 213912 >> >> If I try to simply print (put) it: >> puts myinitialdbcount.count_table{ |data| p data } >> >> This returns: >> nill >> >> I think I need to "to_int" this object or get it into a state where I >> can assign it to a local variable... There is something I dont >> understand here. >> >> dbutil.rb: >> def count_table >> dbh= DBI.connect(@mydb, @dbuser, @dbpw) >> sth = dbh.execute("SELECT count(*) FROM #@table where type_code = >> #@type_code") >> row = sth.fetch >> yield row[0] >> sth.finish >> dbh.disconnect >> end >> >> > I don't know anything about DBI, but what do you get as output from the "p > data" statement? > > Also, I noticed a problem with your dbh.execute statement: "SELECT ... > #@table ..." probably isn't doing what you were hoping it was doing. You > most likely meant "SELECT ... #{@table} ...", also with "#{@type_code". > > hth > --wpd > > The OP's approach works as well. It's a shortcut which can be used with instance variables and global variables: irb(main):001:0> @a = "hello" => "hello" irb(main):002:0> "#@a there" => "hello there" irb(main):003:0> $a = "hi" => "hi" irb(main):004:0> "#$a back" => "hi back" -Justin