# How to pass parameters to a script in "lib" from terminal

**URL:** <https://www.ruby-forum.com/t/how-to-pass-parameters-to-a-script-in-lib-from-terminal/157358>\
**Category:** Rails\
**Created:** [January 9, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-to-a-script-in-lib-from-terminal/157358 "2009-01-09T21:49:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [January 9, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-to-a-script-in-lib-from-terminal/157358/1 "2009-01-09T21:49:10Z")

</div>

Hi,

This should be easy, for some reason I can’t remember how to do it.

I have in /lib a file called “joe\_converter.rb” that I want to be able  
to run from terminal. Here’s the beginning of “joe\_converter.rb”:

* * *

require ‘active\_record’

# Takes in a URL, creates all needed objects, and ends up creating an

LqSnapshot for that URL  
class JoeConverter  
def self.do\_the\_conversion url, scores, user\_id

* * *

How can I run this from terminal correctly? I tried with  
“script/runner”, and put in the necessary parameters, but I get back  
this:

wrong number of arguments (0 for 5) (ArgumentError)

Anyone have any ideas about this?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [January 9, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-to-a-script-in-lib-from-terminal/157358/2 "2009-01-09T21:58:00Z")

</div>

I don’t think you can do it using script/runner (I may be wrong).  
You could run the script directly using ruby lib/joe\_converter.rb, and  
handle the parameters in your script. Your rails app wouldn’t load in  
this case. Another (probably preferred) option is to create a rake  
task in lib/tasks that takes the parameters and calls  
JoeConverter.do\_the\_conversion passing the parameters.

If you are accessing any other classes/modules of your rails app (like  
any model for instance), go with the rake task as they would load as  
well.

Someone else might have better ideas though 😉

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [January 9, 2009, 10:21pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-to-a-script-in-lib-from-terminal/157358/3 "2009-01-09T22:21:58Z")

</div>

I’ve put it in a raketask, works now, except for one thing; if I try to  
pass a hash to it as a parameter, it seems like it just stops. Hmm…

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [January 9, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/how-to-pass-parameters-to-a-script-in-lib-from-terminal/157358/4 "2009-01-09T22:33:19Z")

</div>

Well, you have to play nice with the “rules” of the command line (like  
no  
spaces etc). Also note that whatever comes into your rake task is simply  
a  
string. You would have to parse that string and build any object you  
need,  
such as a hash or array…

Any code on how you’re handling the params?

On Fri, Jan 9, 2009 at 4:21 PM, Joe P.
