[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

noob question: object to string

Darin Ginther

1/5/2009 4:02:00 PM

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

7 Answers

Todd Benson

1/5/2009 4:29:00 PM

0

On Mon, Jan 5, 2009 at 10: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

The count_table method will return whatever the last statement in the
method returns, which, in this case is dbh.disconnect. My guess is
that disconnect always returns nil unless you are already
disconnected. So, in essence, you are saying, puts nil. It should
still print the data to your output stream (most likely standard
output), though, because that's what your block is doing with the
yielded value row[0].

Todd

Darin Ginther

1/5/2009 4:35:00 PM

0




I understand what you are saying... We used "yield" to return from the
method, otherwise I was essentially returning the dbh.disconnect.

Calling the method is writing to standard out... My question is, how do
I get that standard output into a local variable or some other usable
form? I need to assign it to a variable and then compare it to another
variable later...
--
Posted via http://www.ruby-....

Jim Menard

1/5/2009 5:11:00 PM

0

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-....
>
>



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

Darin Ginther

1/5/2009 5:12:00 PM

0

Got it. Yield goes straight to stdio. Instead, return the first value
of that array at the end of the method:
def count_table
dbh= DBI.connect(@mydb, @dbuser, @dbpw)
sth = dbh.execute("SELECT count(*) FROM #@table where
lead_type_code = #@lead_type_code")
row = sth.fetch
#yield row[0] This will write to STIO immediately
sth.finish
dbh.disconnect
row[0]
end
end
--
Posted via http://www.ruby-....

Patrick Doyle

1/5/2009 8:07:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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

Darin Ginther

1/5/2009 8:14:00 PM

0

Was working, but I'll take the context correction.
--
Posted via http://www.ruby-....

Justin Collins

1/7/2009 6:12:00 AM

0

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