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 }

A block returns the value of the last expression inside it. The last
expression here is "p data", so what is getting returned is the value
returned by the "p" method. Try this:

initialcount = myinitialdbcount.count_table{ |data| data }
# Now initialcount contains the value in data
p initialcount
# or
puts initialcount.to_s

>
> 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
> --
> Posted via http://www.ruby-forum.com/.
>
>



-- 
Jim Menard, jimm / io.com, jim.menard / gmail.com
http://www.io.com/~jimm/