[SOLVED] How to insert separator within printf in ruby
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well you could do it that way ... you might want to do some checking around how many parameters were passed to the script otherwise less that 2 will yield odd results and perhaps errors.
As for your original question, I would have said the easiest way would be:
Thanks for your help. Following your suggestion I've used
print [$1,$2,$3].join(sep) instead of puts since puts inserts a end of line.
Regarding the options to pass to the script, may you suggest me a way to handle when parameters are passed or not
in the console. I want to use 2 optional parameters (-a and -b and their respective values) and a mandatory one
that is the input file.
To run the script would be like this:
Code:
Script.rb -a "," -b "@" input
Is there a better way than set several "if" conditions to test if one, two or all parameters where passed?
This is how looks what I have so far.
Code:
if ARGV[0][0..1] == "-a" and ARGV[1].length > 0 and ARGV[2][0..1] == "-b" and ARGV[3].length > 0 then
...
end
For parameter checking, I would suggest a similar tactic as with other languages:
1. test number of parameters passed to script (ARGV.length)
2. loop over each one using either 'if' (one or two options) or 'case' (multiple) to check and apply the switches as required
Another idea, based on your current example may also be to test that ARGV.length is always odd (ie. 1, 3, 5) cause if the user were to pass
2 arguments and they were '-a' and the input file, this would cause your if to still be correct but for the script to behave very strangely
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.