Ruby: How to Load .Rb File in The Local Context

ruby: how to load .rb file in the local context

You certainly could hack out a solution using eval and File.read, but the fact this is hard should give you a signal that this is not a ruby-like way to solve the problem you have. Two alternative designs would be using yaml for your config api, or defining a simple dsl.

The YAML case is the easiest, you'd simply have something like this in main.rb:

Class App
def loader
config = YAML.load('config.yml')
p config['var'] # => "val"
end
end

and your config file would look like:

--- 
var: val

How to run a ruby script within bundler context?

Pass the script name to the ruby command:

bundle exec ruby script_name

If you also want the Rails environment:

bundle exec rails runner script_name

Accessing a variable declared in another rb file

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.

How can I access a variable defined in a Ruby file I required in IRB?

While it is true that you cannot access local variables defined in required files, you can access constants, and you can access anything stored in an object that you have access to in both contexts. So, there are a few ways to share information, depending on your goals.

The most common solution is probably to define a module and put your shared value in there. Since modules are constants, you'll be able to access it in the requiring context.

# in welcome.rb
module Messages
WELCOME = "hi there"
end

# in irb
puts Messages::WELCOME # prints out "hi there"

You could also put the value inside a class, to much the same effect. Alternatively, you could just define it as a constant in the file. Since the default context is an object of class Object, referred to as main, you could also define a method, instance variable, or class variable on main. All of these approaches end up being essentially different ways of making "global variables," more or less, and may not be optimal for most purposes. On the other hand, for small projects with very well defined scopes, they may be fine.

# in welcome.rb
WELCOME = "hi constant"
@welcome = "hi instance var"
@@welcome = "hi class var"
def welcome
"hi method"
end

# in irb
# These all print out what you would expect.
puts WELCOME
puts @welcome
puts @@welcome
puts welcome

Pass ruby script file to rails console

In the meantime, this solution has been supported.

rails r PATH_TO_RUBY_FILE

Much simpler now.



Related Topics



Leave a reply



Submit