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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:11:35+00:00 2026-06-10T03:11:35+00:00

this is a begginer question about including .rb files. I would like to have

  • 0

this is a begginer question about including .rb files.

I would like to have access to an array declared in another rb file. My main program goes like this :

#!/usr/bin/env ruby
load 'price.rb'
[...]
max_price = price[az][type] * 2
[...]

and here is the price.rb :

price = {'us-east-1' => {'t1.micro' => 0.02, 'm1.small' => 0.08, 'c1.medium' => 0.165, 'm1.large' => 0.320 },
'us-west-1' => {'t1.micro' => 0.02, 'm1.small' => 0.08, 'c1.medium' => 0.165, 'm1.large' => 0.320 },
'eu-west-1' => {'t1.micro' => 0.02, 'm1.small' => 0.085, 'c1.medium' => 0.186, 'm1.large' => 0.340 }
}

When I run the main script I get this error :

Error: undefined local variable or method `price' for main:Object

What do you think ?

  • 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-10T03:11:37+00:00Added an answer on June 10, 2026 at 3:11 am

    The best way to export data from one file and make use of it in another is either a class or a module.

    An example is:

    # price.rb
    module InstancePrices
      PRICES = {
        'us-east-1' => {'t1.micro' => 0.02, ... },
        ...
      }
    end
    

    In another file you can require this. Using load is incorrect.

    require 'price'
    
    InstancePrices::PRICES['us-east-1']
    

    You can even shorten this by using include:

    require 'price'
    
    include InstancePrices
    PRICES['us-east-1']
    

    What you’ve done is a bit difficult to use, though. A proper object-oriented design would encapsulate this data within some kind of class and then provide an interface to that. Exposing your data directly is counter to those principles.

    For instance, you’d want a method InstancePrices.price_for('t1.micro', 'us-east-1') that would return the proper pricing. By separating the internal structure used to store the data from the interface you avoid creating huge dependencies within your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question in here, and I would like to ask if
This question is directly related to this SO question I posed about 15 minutes
I am just begginer so sorry for my silly question. my model looks like
this is a really basic question I know, I am a begginer in Haskell.
I would like to do the following thing : - Loading an array (meaning
I have spent a lot of time to resolve this problem. I'm begginer in
I'm C++ begginer. I did this excercise from Deitel's book: Use a one-dimensional array
I have a question about receiving emails from a smtp server. So I must
This is mostly a philosophical question about the best way to interpret the HTTP
This is a normalization thing, but I want I have to hold information about

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.