From: Darin Ginther Date: 2008-12-30T05:05:44+09:00 Subject: Creating a class for returing DB results I'm a ruby noob. I've managed to accomplish what I want to do in one long procedural chain of code. I'm attempting to modularize my code to prevent duplication, but I'm having trouble with syntax. What I'm doing: Manipulating a web page and then doing simple DB validation of the results. I do all of this in a ruby script. What I want to do: Manipulate web page, then call a validation class, passing that class: DBName UserName UserPassword return: Single value (numeric). I'm having trouble with two things: 1) Generic syntax 2) Return value - because Ruby auto returns the value of the last expression, I have to explicitly return what I'm after, which seems to drop me out out the class, prior to terminating the DB connection. What I essentially have currently is: dbh= DBI.connect(mydb,dbuser,dbpw) sth = dbh.execute("SELECT count(*) FROM #{table} where code = #{mycode}") while row = sth.fetch do finalcount=row[0] puts finalcount end sth.finish # disconnect from server dbh.disconnect if dbh So, roughly what I'm thinking is: 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 dbh= DBI.connect(mydb,dbuser,dbpw) sth = dbh.execute("SELECT count(*) FROM #{table} where code = #{mycode}") while row = sth.fetch do finalcount=row[0] #How do I return this value? end sth.finish # disconnect from server dbh.disconnect if dbh end I think we can then instantiate this object via: mydbcount = Countdb.new(val1, val2, val3, val4, val5) I'm sure I'm making a mess here... straighten me out guys.. -- Posted via http://www.ruby-forum.com/.