From: Brian Candler Date: 2008-12-30T05:45:59+09:00 Subject: Re: Creating a class for returing DB results You're close. Wrap the actual code which does the action in another instance method, such as 'process_rows'. Then to 'return' multiple values, the Ruby way is to yield them to a block which the caller passes. > Class Countdb > attr_accessor :mydb, :dbuser, :dbpw, :table, :mycode > > require 'DBI' > > def initialize(mydb, dbuser, dbpw, table, mycode) > @mydb = mydb > @dbuser = dbuser > @dbpw = dbpw > @table = table > @mycode = mycode > end def process_rows > > dbh= DBI.connect(mydb,dbuser,dbpw) change these to @mydb, @dbuser, @dbpw (ditto table to @table and mycode to @mycode below) > sth = dbh.execute("SELECT count(*) FROM #{table} where code = > #{mycode}") > while row = sth.fetch do > finalcount=row[0] #How do I return this value? yield row[0] > end > sth.finish > # disconnect from server > dbh.disconnect if dbh end > end > I think we can then instantiate this object via: > mydbcount = Countdb.new(val1, val2, val3, val4, val5) That creates an instance of the object and sets the instance variables such as @mydb etc. After this the caller would then do something like this: mydbcount.process_rows { |data| p data } -- Posted via http://www.ruby-forum.com/.