argv defines

M

Michael Linfield

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
 
J

Jano Svitok

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])
 
R

Robert Klemme

2007/11/16 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top