How to Reference a Method in Another Ruby Code File

How to reference a method in another Ruby code file?

If you are using Ruby 1.9 or later, this is the simplest way to do it:

require_relative 'somelogic'

If you want your code to work in 1.9 and older versions of Ruby, you should do this instead:

require File.join File.dirname(__FILE__), 'somelogic'

Whichever line you choose, you should put it at the top of your ruby file. Then any classes, modules, or global variables defined in somelogic.rb will be available to your program.

How do I call a method that's in one Ruby script from another?

Modules are built for organizing methods and will be happy to be included. If you included a module, you no longer need the namespace.

require_relative "shared"
include CommonMethods
createLog # Call it simply

How to access class methods from another file

The methods are accessible in the first file. In essence, doing the require is the same as writing it all in the same file.

By doing just that, you can see part of your problem:

require "selenium-webdriver"

def save
driver.save_screenshot('screenshot.png')
end

def hello
puts "hello"
end

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://www.google.com"

hello
save

The variable driver is not available inside of the method save.

One way around this is to let save take the driver as an argument:

def save(driver)
driver.save_screenshot('screenshot.png')
end

# Call the method like this
save(driver)

Some points:

  • Writing code in the global scope like this is not advisable. Try wrapping what you want to do in a class.
  • The methods you've defined aren't really class methods. As @JörgWMittag pointed out: they become private instance methods of Object

Calling a function in other file in Ruby

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

How to call a method from a module of an other ruby file

Require needs the absolute path to the file unless the file is located in one of Ruby's load paths. You can view the default load paths with puts $:. It is common to do one of the following to load a file:

Add the main file's directory to the load path and then use relative paths with require:

$: << File.dirname(__FILE__)
require "my_module"

Ruby 1.8 code that only loads a single file will often contain a one-liner like:

require File.expand_path("../my_module", __FILE__)

Ruby 1.9 added require_relative:

require_relative "my_module"

In the module you will need to define the methods as class methods, or use Module#module_function:

module MyModule
def self.method1 ary
...
end

def method2
...
end
module_function :method2
end

a = [1,2,3,4]
MyModule.method1(a)

In Ruby, can I reference a lambda defined in another file?

Variables starting with lowercase letters are local variables. Local variables are local to the scope they are defined in (that's why they are called "local" variables).

In your example, add_links_to_descriptions is local to the script B.rb, it can only be accessed within the script scope of B.rb.

You have to use something other than a local variable: an instance variable of the object that is trying to call the lambda, a method of an object that is accessible to the object trying to call the lambda, a constant, a global variable are some of the possibilities. Which one of those is the "right one" depends on the overall design of your project.



Related Topics



Leave a reply



Submit