user2823083
user2823083

Reputation: 445

Calling a function in other file in Ruby

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?

Upvotes: 1

Views: 1091

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

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

Upvotes: 2

Related Questions