[SOLVED] RUBY: How to assign puts output to a variable
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.
In my ruby script below lines displays IPs which I am reading from a file.
Code:
puts line.split[0] if line.match(/^[0-9]/)
Something like this if it was bash
Code:
var=`puts line.split[0] if line.match(/^[0-9]/)`
func($var)
I am looking a way to get the output in a variable and then pass as an argument to be used in a function. I have tried few options from Google in vain.
Forgive my ignorance on this. I am not very well versed with Ruby as of now.
I'm not a Ruby programmer (Python right now), but things tend to be transferable across languages.
Remember that puts is just printing to the screen; it's not returning a value. What you're printing to the screen is line.split[0] for the case where that regex matches.
So, you just want to do the same, i.e.
Code:
str = ""
if line.match(/^[0-9]/)
str = line.split[0]
Break tasks down into smaller steps and then work out how to do each step.
No idea, I'm afraid. I didn't realise '$' was used for variables in Ruby (at least sometimes, from your post), so I'm out of my depth now. I'd suggest reading up more on functions and variables in the language and maybe consult a dedicated Ruby forum (I'm not sure how many Ruby programmers are on LQ).
I'm not sure how you're learning Ruby, but maybe the Ruby Koans are good: http://rubykoans.com/.
No idea, I'm afraid. I didn't realise '$' was used for variables in Ruby (at least sometimes, from your post), so I'm out of my depth now. I'd suggest reading up more on functions and variables in the language and maybe consult a dedicated Ruby forum (I'm not sure how many Ruby programmers are on LQ).
I'm not sure how you're learning Ruby, but maybe the Ruby Koans are good: http://rubykoans.com/.
No worries mate. I do not know why/how, but this worked.
Code:
if line.match(/^[0-9]/)
str = line.split[0]
func(str)
end
I am glad, I learnt something today. Thanks for your help and patience.
In general I would say, if you are going to learn a language then have a look at some tutorials and at least try to get the basics down.
You are looking at this question by, initially, trying to bend it to do what you do elsewhere and hence getting the wrong results.
Furthermore, if you do look up some tutorials you will see that #{} is only needed to expand a variable inside quotes and seeing as you have nothing else in the quotes,
both the quotes and #{} are not needed. Also, $ if for global variables so as a good practice you should get out of the habit of needing them.
Lastly, your question is very ill-formed and by asking it piecemeal it has slowed the answering process. In future, ask what you need, and show examples using informative data.
Furthermore, if you do look up some tutorials you will see that #{} is only needed to expand a variable inside quotes and seeing as you have nothing else in the quotes,
both the quotes and #{} are not needed. Also, $ if for global variables so as a good practice you should get out of the habit of needing them
Hi Grail,
I did looked on some tutorials, may be I had missed this point. I will to try be more explanative next time. Thanks.
I am following this tutorial now, looks very well organized to me. I hope this will be helpful for others who are on the same learning curve.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.