Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference an array from one ruby script to another

Tags:

ruby

Can we reference an array from one ruby script to another and access array elements?

for example : my first rb file

1.rb

$joe = "one"
$po = "two"
$so  = "three"
names = [ $joe, $po, $so ]

second rb file

2.rb

require "1"
$trial = names[1]
puts $trial

But this didn't work.

like image 856
wanderors Avatar asked May 23 '26 09:05

wanderors


1 Answers

You can do it like this ( Ruby 1.9 ):

1.rb:

module Whatever
    @names = ["one","two","three"]

    def self.names
        @names
    end
end

2.rb:

require_relative "1"

Whatever.names.each {|n| puts n}
like image 173
Geo Avatar answered May 26 '26 03:05

Geo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!