# Argv defines

**URL:** <https://rubytalk.org/t/argv-defines/42262>\
**Category:** ruby-talk\
**Created:** [16 November 2007 20:31 UTC](https://rubytalk.org/t/argv-defines/42262 "2007-11-16T20:31:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Hawkins](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@Jon\_Hawkins](https://rubytalk.org/u/Jon_Hawkins)\
**Post date:** [16 November 2007 20:31 UTC](https://rubytalk.org/t/argv-defines/42262/1 "2007-11-16T20:31:09Z")

</div>

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  
&nbsp;&nbsp;class \<\< self

def join\_group(newgroup)  
&nbsp;&nbsp;@@base = PGconn.connect("localhost",5432,"","","base","god","letmein")  
if newgroup == "Group\_1"  
&nbsp;&nbsp;&nbsp;@@base.exec("insert into GROUP\_1(members) values ('#{@@client}')")  
elsif newgroup == "Group\_2"  
&nbsp;&nbsp;&nbsp;@@base.exec("insert into GROUP\_2(members) values ('#{@@client}')")  
elsif newgroup == "Group\_3"  
&nbsp;&nbsp;&nbsp;@@base.exec("insert into GROUP\_3(members) values ('#{@@client}')")  
else  
&nbsp;&nbsp;puts "Error: Non-Existance Group. Check Spelling."  
&nbsp;&nbsp;end  
end  
end

Thanks,

- Mac

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [16 November 2007 22:52 UTC](https://rubytalk.org/t/argv-defines/42262/2 "2007-11-16T22:52:48Z")

</div>

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  
&nbsp;&nbsp;def self.connect(\*args); return new end  
&nbsp;&nbsp;def exec(\*args) ; p args ; end  
end

class User  
&nbsp;&nbsp;@@client = 'client' # dummy

&nbsp;&nbsp;GROUPS = %w{ Group\_1 Group\_2 Group\_3 }

&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;def join\_group(newgroup)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 1. ||= -\> connect only once  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# if you need @@base only in singleton (class) methods, just use  
@base - singleton instance variable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# this will not be inherited to derived classes though.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@base ||= PGconn.connect("localhost",5432,"","","base","god","letmein")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case newgroup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when \*GROUPS # this is equivalent to: when "Group\_1", "Group\_2", "Group\_3"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@base.exec("insert into #{newgroup.upcase}(members) values  
('#{@@client}')")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Error: Non-Existant Group. Check Spelling."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

# ./script.rb 'User.join\_group("Group\_1")'  
eval(ARGV[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?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [17 November 2007 16:30 UTC](https://rubytalk.org/t/argv-defines/42262/3 "2007-11-17T16:30:03Z")

</div>

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

ruby -r script -e 'User.join\_group("Group\_1")'

Kind regards

robert

> **···**
>
> 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).
> 
> --  
> use.inject do |as, often| as.you\_can - without end
