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 7191777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:48:05+00:00 2026-05-28T19:48:05+00:00

This is a pretty simple question. def func1 t2=Thread while true # if t2.alive?

  • 0

This is a pretty simple question.

def func1
  t2=Thread
    while true
      # if t2.alive?                   
      #   puts "GUI is running"
      # end
      puts Thread.t2.stop?
      puts "func1 at: #{Time.now}"
      sleep(1)
    end
end

t1=Thread.new{func1()}
t2=Thread.new{TheGUI()}
t1.join
t2.join

t2 is only declared later on in the code, so I am getting errors when trying to run this.
The error is ‘undefined local variable or method `t2”

How can I fix this without reordering my code?

Thanks

  • 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-28T19:48:06+00:00Added an answer on May 28, 2026 at 7:48 pm

    Your snippet is pretty small, so it’s hard to tell if your code is at top-level or in a class.

    If t2 is supposed to be a global variable, note that Ruby prefixes global variables with a $: $t2.

    If t2 is supposed to be a class member, note that Ruby prefixes member variables with a @: @t2.

    Update

    Your updated code is making an alias for the Thread class named t2. Check this output:

    $ irb
    irb(main):001:0> t2=Thread
    => Thread
    irb(main):002:0> t2
    => Thread
    irb(main):003:0> t2.methods()
    => ["private_class_method", "inspect", "name", "stop", "tap", "clone", "public_methods", "__send__", 
    ...
    irb(main):004:0> Thread.methods()
    => ["private_class_method", "inspect", "name", "stop", "tap", "clone", "public_methods", "__send__", 
    

    Furthermore, that t2 alias is only in force in the scope of the func1 function definition.

    The simplest way to amend your code is probably to change func1 to take a parameter:

    def func1(second_thread)
      while second_thread.alive?
        puts "GUI is running"
        sleep 1
      end
    end
    
    t2 = Thread.new {TheGUI()}
    # pass the parameter to the function here
    t1 = Thread.new(t2) { |thread| func1(thread) }
    t1.join()
    t2.join()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty simple question but I'm somewhat stumped. I am capturing sections
I think this is a pretty simple question. How do you an apache rewrite
I'm pretty sure this is a simple question in regards to formatting but here's
This a pretty simple question (I assume). Probably a repost, but I couldn't find
This is a pretty simple question, clearly it's an issue either with my CSS
This is a pretty simple question- how can I check if a NSURL is
This is a pretty simple question really, but let's say I'm creating a model
Hopefully this is a pretty simple question for someone to answer. I have a
New to Javascript and jQuery, so I assume this is a pretty simple question.
This might be a pretty simple question, but if there really is a difference,

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.