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

  • SEARCH
  • Home
  • 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 8899283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:49:14+00:00 2026-06-15T00:49:14+00:00

I am working on my small newbie project. Project consist of two scripts. Script

  • 0

I am working on my small newbie project. Project consist of two scripts. Script number1 is command line interface which recive parameters from user. Script number 2 creates Cartesian product and write it into text file. My idea is to make everything working without puting evertything into one file). When I try to use ‘load’ I get this error:”

Carthese_product.rb:3:in `<top (required)>': undefined local variable or method
`x_min' for main:Object (NameError)
        from D:/Cli_file.rb:25:in `load'
        from D:/Cli_file.rb:25:in `<main>'

Script1 (Cli_file.rb):

  require 'trollop'
  opts = Trollop::options do
  banner <<-EOS
Welcome to points generator!

Usage:
       test [options] <filenames>+
where [options] are:
EOS
    opt :x_min,             "Minimal value of X", :type => :int  
    opt :x_max,             "Maximal value of X", :type => :int 
    opt :y_min,            "Minimalna wartosc Y", :type => :int 
    opt :y_max,             "Maksymalna wartosc Y", :type => :int 
    opt :interval,          "Interval between points", :type => :int, :default => 1 
    opt :products_file,     "Name of products_file", :type => :string, :default =>    
                            "Products"     
  end

a= opts
x_min = a[:x_min]
x_max = a[:x_max]
y_min = a[:y_min] 
y_max = a[:y_max]
interval = a[:interval]
products_file = a[:products_file]
load 'Carthese_product.rb'

Script2 (Carthese_product.rb)

products = []

(x_min/interval..x_max/interval).each do |x|
(y_min/interval..y_max/interval).each do|y|
products << [x*interval,y*interval]
end
end
a = products.map.with_index{|w, i| "#{i+1} #{w[0].to_s} #{w[1].to_s} \n"}

aFile = File.new( products_file, "w+")
if aFile
    a.each{|x| aFile.write(x)}
else
puts "Something wrong!"
end

I know the easiest sollution is to put everything into one script, but for my educational purposes I want to find another way! Thanks for help & intrest!

  • 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-06-15T00:49:15+00:00Added an answer on June 15, 2026 at 12:49 am

    You are attempting to use local variables to pass data from one script to the other. It doesn’t work because local variables, when defined at the top level, have file scope and thus cannot be accessed from separate files.

    You must create a proper interface to your code so that you can reference it from other scripts. Create a module which implements Cartesian product:

    # cartesian.rb
    
    module Cartesian
      extend self
    
      def product(x_range, y_range, interval = 1)
        [].tap do |products|
          x_range.step interval do |x|
            y_range.step interval do |y|
              products << [x, y]
            end
          end
        end
      end
    end
    

    Now, require this file in your command line application executable, use the data given in the command line and write your output:

    #/usr/bin/env ruby
    require 'cartesian'
    
    # Option parsing
    
    Cartesian.product(x_min..x_max, y_min..y_max, interval).each do |product|
      puts "(#{product.first}, #{product.last})"
    end
    

    I recommend printing the program’s output to the standard output stream. That way, you can easily redirect the output to a file if you want to:

    ./cartesian-product $ARGUMENTS > product.list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One really newbie question. I'm working on a small python script for my home
I am working on small application of sharepoint. There two ways in which WE
I am working on small python payroll project where you enter employee name, wage,
I'm working on a small uni project where I have a program for a
I'm working on small open source project for developed Windows. I created new project
I'm working for small company, which operates in the automation industry. The boss hired
Working on a small web application, I have a user table in which user
Im working on a small graphics engine project that and I want to it
Im working on a small PHP project for learning purpose. i want to retrieve
I'm (a newbie in php) still working on a time off project and another

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.