Argv defines

if i wanted to ARGV this, how would i do it?
IE from the command line i would want to do as such —
./script.rb User.join_group(“Group_1”)

Any ideas?

class User
class << self

def join_group(newgroup)
@@base = PGconn.connect(“localhost”,5432,"","",“base”,“god”,“letmein”)
if newgroup == “Group_1”
@@base.exec(“insert into GROUP_1(members) values (’#{@@client}’)”)
elsif newgroup == “Group_2”
@@base.exec(“insert into GROUP_2(members) values (’#{@@client}’)”)
elsif newgroup == “Group_3”
@@base.exec(“insert into GROUP_3(members) values (’#{@@client}’)”)
else
puts “Error: Non-Existance Group. Check Spelling.”
end
end
end

Thanks,

  • Mac

On Nov 16, 2007 9:31 PM, Michael L. [email protected]
wrote:

if i wanted to ARGV this, how would i do it?
IE from the command line i would want to do as such —
./script.rb User.join_group(“Group_1”)

Any ideas?

One way is to use eval. Other way is to define special commands
(–join_group=Group_1).
On windows I had to enclose the argument in single quotes to preserve
the double ones.

dummy implementation

class PGconn
def self.connect(*args); return new end
def exec(*args) ; p args ; end
end

class User
@@client = ‘client’ # dummy

GROUPS = %w{ Group_1 Group_2 Group_3 }

class << self
def join_group(newgroup)
# 1. ||= → connect only once
# if you need @@base only in singleton (class) methods, just use
@base - singleton instance variable
# this will not be inherited to derived classes though.
@base ||=
PGconn.connect(“localhost”,5432,“”,“”,“base”,“god”,“letmein”)
case newgroup
when *GROUPS # this is equivalent to: when “Group_1”, “Group_2”,
“Group_3”
@base.exec(“insert into #{newgroup.upcase}(members) values
(‘#{@@client}’)”)
else
puts “Error: Non-Existant Group. Check Spelling.”
end
end
end
end

./script.rb ‘User.join_group(“Group_1”)’

eval(ARGV[0])

2007/11/16, Jano S. [email protected]:

On Nov 16, 2007 9:31 PM, Michael L. [email protected] wrote:

if i wanted to ARGV this, how would i do it?
IE from the command line i would want to do as such —
./script.rb User.join_group(“Group_1”)

Any ideas?

One way is to use eval. Other way is to define special commands
(–join_group=Group_1).

Yet another solution is to do it directly on the command line:

ruby -r script -e ‘User.join_group(“Group_1”)’

Kind regards

robert