LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-22-2014, 06:58 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Question RUBY: How to assign puts output to a variable


Dear All,

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.

Last edited by vikas027; 12-22-2014 at 07:00 AM.
 
Old 12-22-2014, 07:07 AM   #2
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
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.
 
Old 12-22-2014, 07:35 AM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by ndc85430 View Post
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.
Thanks for the quick response, I tried this with no luck.

Code:
  str = ""
  if line.match(/^[0-9]/)
     
     ## Works well but displays on screen
     puts line.split[0]
     
     ## Does Not Works
     str = line.split[0]
     puts #{str}
  
  end
 
Old 12-22-2014, 07:40 AM   #4
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
I believe you need to quote the string that contains your variable, so that

puts #{str}

should be

puts "#{str}"

This works for me. Again, I'm not a Ruby programmer, so don't know the ins and outs of the language.

Edit: FWIW, you can also try things out at the interactive prompt - run irb in a terminal.

Last edited by ndc85430; 12-22-2014 at 07:42 AM.
 
1 members found this post helpful.
Old 12-22-2014, 08:07 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Thumbs up

Quote:
Originally Posted by ndc85430 View Post
I believe you need to quote the string that contains your variable, so that

puts #{str}

should be

puts "#{str}"

This works for me. Again, I'm not a Ruby programmer, so don't know the ins and outs of the language.

Edit: FWIW, you can also try things out at the interactive prompt - run irb in a terminal.
Perfect. You should start programming in ruby too.

Many thanks for the help. This works just fine.

Code:
  if line.match(/^[0-9]/)
    str = line.split[0]
    puts "#{str}"
  end
But when I try to pass variable 'myip' in a function, the value does not gets passed.

Code:
  if line.match(/^[0-9]/)
    str = line.split[0]
    puts "#{str}"
    
    func("#{str}")
    or
    func($str)
  
  end
This just works just fine, when I ask the the user interactively.

Code:
print "Enter IP - "
$stdout.flush
$ip = STDIN.gets.strip
func ($ip)
 
Old 12-22-2014, 08:34 AM   #6
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
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/.
 
1 members found this post helpful.
Old 12-22-2014, 09:13 AM   #7
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Thumbs up

Quote:
Originally Posted by ndc85430 View Post
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.
 
Old 12-22-2014, 10:53 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.

As a guide you may also find the following useful :- https://github.com/bbatsov/ruby-style-guide
 
1 members found this post helpful.
Old 12-23-2014, 08:07 PM   #9
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Original Poster
Rep: Reputation: 107Reputation: 107
Lightbulb

Quote:
Originally Posted by grail View Post
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.

Last edited by vikas027; 12-24-2014 at 03:43 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[BASH] Assign output to variable(s) useretail Linux - Newbie 4 12-06-2014 06:08 AM
Can not assign variable to output of sed protocol Linux - General 5 10-24-2011 04:36 PM
How to assign a variable to the output value of a program BeyondSora Linux - Newbie 3 02-07-2011 12:46 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 06:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration