I’m trying to pass a variable’s definition of a script from another ruby
script or irb.
That is
some_file.rb contains
some_var = “Hello, world!”
DL is deprecated, please use Fiddle
irb(main):001:0> load ‘some_file.rb’
=> true
irb(main):002:0> puts some_var
NameError: undefined local variable or method some_var' for main:Object from (irb):2 from C:/Ruby200/bin/irb:12:in’
I know you can do this by using methods but is there any thing I need to
know when pulling this off?
def some_method
$some_var = “Hello, world!”
end
I could just create a file, save the var’s definition in it but this
time I really need a change.
It is the responsibility of “the other” script to make available the
variable in question. If you cannot work with global variables, you have
to hand over the value in a dedicated routine.
Usually, such variables are attributed to objects which are accessible
to those functions or methods, which need to have this access.
If you are not yourself the author of “the other” script, you should
contact the person who is and ask her/him for an explanation and,
possibly, a suggestion.
Yes but I don’t know how to make the var available to another script.
Take your documentation (book, online-tutorial or other) and read about
the two commands
require
require_relative
To make available any symbols from one file to the code, stored in a
different file, the second must “require” the file that contains the
sought symbols.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.