1

I have a hash like this in file1.rb

#file1.rb

h1 = {"k1"=>"v1", "k2"=>"75.1%"}

formatting (h1) #Function in file2.rb

From this file, I want to call a function in file2.rb and pass this hash h1

#file2.rb

def formatting(h1)
.
.
.
end

How can I do it in Ruby?

user2823083
  • 445
  • 2
  • 4
  • 10
  • We need to see your attempts beyond those tiny code outlines. Show how you're trying to load the second file. Do you get errors? "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See http://SSCCE.org for guidance. " This is answered in http://stackoverflow.com/questions/2900370/why-does-ruby-1-9-2-remove-from-load-path-and-whats-the-alternative. – the Tin Man Sep 29 '13 at 18:20

1 Answers1

2

You can use the method Kernel#require_relative.

I assumed here that both the files are under same directory /home/kb/Ruby.

file1.rb

require_relative 'file2.rb'
h1 = {"k1"=>"v1", "k2"=>"75.1%"}
formatting (h1)

file2.rb

def formatting(h1)
 #code
end
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317