Jano Svitok
11/16/2007 10:53:00 PM
On Nov 16, 2007 9:31 PM, Michael Linfield <globyy3000@hotmail.com> 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])