Using a Hash as an Argument for a Function or Method

I’m new to writing RubyGems and I wanted to include a gem_name/bin file
and have it accept command line arguments then pass them to the
gem_name/lib/gem_name.rb file for further processing and just hand it
completely off after that. I’m using a Hash, and OptionParser to do my
option command line options. So, I have my Hash in my
gem_name/bin/gem_name file, but need to pass them on to the
gem_name/lib/gem_name.rb file.
After reading this:

I thought about still using the OptionParser, and the Hash, but then
passing the whole Hash as a single argument instead of doing each
key,value pair.

I hope this made sense. My directory structure is:

gem_name
|-bin
||-gem_name
|
|-lib
||-gem_name.rb
||-gem_name
|||-version.rb
|
|-test
||-test.rb
|
|-gem_name.gemspec

On Wed, Nov 20, 2013 at 5:46 AM, Philip D. [email protected] wrote:

passing the whole Hash as a single argument instead of doing each
key,value pair.

I hope this made sense. My directory structure is:

I am not really sure why you mention the directory structure. Isn’t
this all about requiring the proper file and invoking a method which
receives a Hash as argument? e.g.

gem_name/bin/gem_name.rb

require ‘gem_name’
require ‘optparse’

config = {}

OptionParser.new do |opts|

end.parse! ARGV

main = GemClass.new(config)

ARGV.each do |file|
main.process file
end

Kind regards

robert

On Nov 19, 2013, at 8:46 PM, Philip D. [email protected] wrote:

passing the whole Hash as a single argument instead of doing each
||-gem_name
|||-version.rb
|
|-test
||-test.rb
|
|-gem_name.gemspec


Posted via http://www.ruby-forum.com/.

Take a look at some of the other gems that do this sort of thing, thor,
methadone, GLI, and there are others. Do take a look at David Copelands
book “Build Awesome Command Line Programs in Ruby” as well. Good stuff
there.

My apologies gents for not replying sooner. Thank you for those example
Tamouse M., those really helped me learn a lot more.

Robert K., your example was kind of what I was looking for. I wanted
to pass the hash from my bin file to my lib file and then act upon it,
which is just as easy as you displayed and it gave me the idea. I just
require the lib file, then call the module or class function, and then
pass it through to the function itself just like you would with any
other Ruby function. I realize now that this was probably a silly
question for you, but I have it working now, and functions just fine.
Thank again gents.

On Wed, Nov 27, 2013 at 1:33 AM, Philip D. [email protected] wrote:

My apologies gents for not replying sooner. Thank you for those example
Tamouse M., those really helped me learn a lot more.

No problem.

Robert K., your example was kind of what I was looking for. I wanted
to pass the hash from my bin file to my lib file and then act upon it,
which is just as easy as you displayed and it gave me the idea. I just
require the lib file, then call the module or class function, and then
pass it through to the function itself just like you would with any
other Ruby function. I realize now that this was probably a silly
question for you, but I have it working now, and functions just fine.

Good! Btw. this is not silly at all, I was just a bit confused whether
I was missing something.

Thank again gents.

You’re welcome!

Kind regards

robert