Access to a variable declared in another rb file

This is a question about including .rb files.

I would like to have access to the array declared in another rb file. My main program is as follows:

#!/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?

+6
source share
5 answers

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

Example:

 # 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 using include :

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

What you did is a little hard to use. A proper object-oriented design will encapsulate this data in some class, and then provide an interface for this. Violation of your data directly contradicts these principles.

For example, you want the InstancePrices.price_for('t1.micro', 'us-east-1') method to return the correct price. By separating the internal structure used to store data from the interface, you avoid creating huge dependencies in your application.

+7
source

Declaring a variable inside a tiny and simple class would be a cleaner imho solution.

+2
source

Quote from the Ruby forum :

Keep in mind that load utilization saves local variables locally in its area in a file.

This means that you can use a price variable only if it is not local; example with an instance variable:

 @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 } } 
+2
source

I think I forgot about the files.

Think of classes and methods.

A couple of options:

  • put these methods and the variable inside the class into a single .rb file.

  • put the variables and method in another file and include them

You will need to think about classes and methods and include and extend in order to have a solution that makes sense.

+1
source

You cannot access the variable from another file , but you can access the function from another file :

file1.rb

 # in case of script you may use 'global' variables (with $) # as they have scope visibility to this whole file $foo = 'bar' def getFoo $foo end 

file2.rb

 require_relative 'file1.rb' foo = getFoo # ... 
0
source

All Articles