Back to main script from another

Hi.

I have mainscript.rb calling a secondscript.rb.When all tasks are done in secondscript.rb i want to back on mainscript.rb.

Here is my mainscript.rb

#!/usr/bin/env ruby


require 'open3'
require 'highline/import'
require 'shellwords'
require 'monitor'
require 'thread'
require 'timeout'
require './checkmutt'


class Super
  def self.test_func
 puts "Go to secondscript.rb"
sleep 20
Mail.messageMail
end
end


class Retour
def self.mainScript
puts "\e[1;36m[*] Back on main script...\e[0m"
Super.test_func
end
end

puts "Welcome !"
Super.test_func

Secondscript.rb is little more complex and work’s and several tasks.I shorted words it.

Secondscript.rb is like that.

#!/usr/bin/env ruby


require 'open3'
require 'highline/import'
require 'shellwords'
require 'monitor'
require 'thread'
require 'timeout'



class Mail
def self.messageMail
puts "Fontion Mail.messageMail"
puts "Passage à Pay.paiementVerif"
Pay.paiementVerif
end

class Pay
def self.paiementVerif
puts "Function Pay.paiementVerif"
puts "Go to Send.envoiFichier"
Send.envoiFichier
end

class Send
def self.envoiFichier
puts "Function Send.envoiFichier"
if #... task one
puts "Fini !"
Retour.mainScript 
elsif #... other tasks
puts "Fini !"
Retour.mainScript 
elsif #... some other tasks
puts "Fini !"
Retour.mainScript 
else
puts "Error !"
end
end
end
end
end

I have the following error when functions are done.

home/koala/Documents/E-book/checkmutt.rb:195:in envoiFichier': uninitialized constant Mail::Pay::Send::Retour (NameError)
	from /home/koala/Documents/E-book/checkmutt.rb:86:in paiementVerif'
	from /home/koala/Documents/E-book/checkmutt.rb:37:in messageMail'
	from mutt.rb:77:in <class:Veille>'
	from mutt.rb:37:in <main>'
`

Any idéa ?

Thanks.

I expect back to main script without errors.

Hey,

It looks like the issue is due to the incorrect use of namespaces. Retour is not defined within the scope of Send, as Send is nested within Pay and Pay is nested within Mail.

Try defining your classes at the top level or use explicit name resolution operator ::. Here is a refactored secondscript.rb:

class Mail
  def self.messageMail
    puts "Fontion Mail.messageMail"
    puts "Passage à Pay.paiementVerif"
    Pay.paiementVerif
  end
end

class Pay
  def self.paiementVerif
    puts "Function Pay.paiementVerif"
    puts "Go to Send.envoiFichier"
    Send.envoiFichier
  end
end

class Send
  def self.envoiFichier
    puts "Function Send.envoiFichier"
    if #... task one
      puts "Fini !"
      ::Retour.mainScript 
    elsif #... other tasks
      puts "Fini !"
      ::Retour.mainScript 
    elsif #... some other tasks
      puts "Fini !"
      ::Retour.mainScript 
    else
      puts "Error !"
    end
  end
end

Here, ::Retour.mainScript refers to Retour class from the top level context.
Happy coding!

Bobby the Bot

Ignoring what the bot said, your example main script is not actually invoking the second script anywhere.

The error message is clearly not from your example scripts. The line numbers and method names from the stack trace do not match your example code. I guess you cut too much from your example?

So the actual problem is not clear. Can you make sure that the example you post has the actual problem and then show the stack trace from the example code? Either that or share the full code. It’s not possible to tell now what the actual issue is.

this is insane! I like this very good

1 Like

Hope you’re doing well! I came across something today that I think you might find useful. It’s a website where you can find specialists dedicated to writing essays. Whether it’s for your master’s thesis or just a regular college assignment, they’ve got a variety of experts ready to help out. Let me know if you want the link to check it out. Could be a game changer for our study sessions