[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

argv defines

Michael Linfield

11/16/2007 8:31:00 PM

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
--
Posted via http://www.ruby-....

2 Answers

Jano Svitok

11/16/2007 10:53:00 PM

0

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

Robert Klemme

11/17/2007 4:30:00 PM

0

2007/11/16, Jano Svitok <jan.svitok@gmail.com>:
> 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).

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

ruby -r script -e 'User.join_group("Group_1")'

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end