Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:37:01+00:00 2026-05-16T23:37:01+00:00

I have a loop where i execute a series of command on a remote

  • 0

I have a loop where i execute a series of command on a remote machine:


   ssh.exec('cd /vmfs/volumes/4c6d95d2-b1923d5d-4dd7-f4ce46baaadc/ghettoVCB;  ./ghettoVCB.sh -f vms_to_backup -d dryrun') do|ch, stream, data|
                                    if #{stream} =~ /vmupgrade/
                                    puts value_hosts + " is " + data
                                    puts #{stream}
                                    puts data
                                    end
                            end

i want to access #{stream} and data outside the do-end loop

I would appreciate any help.
Thanks,

Hi Jörg,

I implemented your suggestions, but now i am getting error:


WrapperghettoVCB.rb:49: odd number list for Hash
      communicator = {ch: ch, stream: stream, data: data}
                         ^
WrapperghettoVCB.rb:49: syntax error, unexpected ':', expecting '}'
      communicator = {ch: ch, stream: stream, data: data}
                         ^
WrapperghettoVCB.rb:49: syntax error, unexpected ':', expecting '='
      communicator = {ch: ch, stream: stream, data: data}
                                     ^
WrapperghettoVCB.rb:49: syntax error, unexpected ':', expecting '='
      communicator = {ch: ch, stream: stream, data: data}
                                                   ^
WrapperghettoVCB.rb:76: syntax error, unexpected kELSE, expecting kEND
WrapperghettoVCB.rb:80: syntax error, unexpected '}', expecting kEND
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:37:02+00:00Added an answer on May 16, 2026 at 11:37 pm

    You can’t. Local variables are local to their scope. That’s why they are called local variables.

    You could, however, use a variable from an outer scope:

    communicator = nil
    
    ssh.exec('...') do |ch, stream, data|
      break unless stream =~ /vmupgrade/
      puts "#{value_hosts} is #{data}", stream, data
      communicator = {ch: ch, stream: stream, data: data}
    end
    
    puts communicator
    

    BTW: there were several bugs in your code, that would have prevented it from working anyway regardless of your problem with variable scoping, because you used wrong syntax for dereferencing local variables: the syntax for dereferencing a variable is simply the name of the variable, e.g. foo, and not #{foo} (that’s simply a syntax error).

    Also, there are some other improvements:

    • formatting: the standard for indentation in Ruby is 2 spaces, not 26
    • formatting: the standard for indentation in Ruby is 2 spaces, not 0
    • formatting: usually, the block arguments are separated from the do keyword with a space
    • guard clause: if you wrap the entire body of a block or method in a conditional, you can just replace that with a guard a the beginning of the block that skips the whole block if the condition is true
    • string interpolation: adding strings together with + is unusual in Ruby; if you need to concatenate strings, you usually do it with <<, but more often than not, string interpolation is preferred
    • multiple arguments to puts: if you pass multiple arguments to puts, it will print all of them on a separate line, you don’t need to call it multiple times
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following command which will loop over all the subdirectories in a
I have a loop that reads each line in a file using getline() :
I have a loop on page to update an access database that takes 15-20
In my WPF client, I have a loop that calls a WCF service to
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
If I have a for loop which is nested within another, how can I
If you have a for loop such as: For Each s As String In
In Java 5 and above you have the foreach loop, which works magically on
I'm trying to solve the 3n+1 problem and I have a for loop that
I have a variable that is built in loop. Something like: $str = ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.