Cedric Vicenti wrote:
> Hi guys,
> 
> my ruby code is now a long file and I'd like to split it into different
> smaller files so that I could reuse them in different programs and make
> my original code smaller at the same time.
> 

1) Here's a simple scheme:


my_lib.rb
--------
def greet
  puts "hello world"
end

def show(num)
  puts num
end



some_program.rb
---------------
require 'my_lib.rb'

greet    #hello world
show(10)   #10


But doing it that way can cause problems in this situation:


some_program.rb
---------------
require 'my_lib.rb'

def greet()
  puts "hi"
end


greet    #hi
show(10)  #10

The greet method in some_program.rb hides the greet method in my_lib.rb. 
So you can do this instead:

my_lib.rb
--------
module Lib
  def Lib.greet
    puts "hello world"
  end

  def Lib.show(num)
    puts num
  end
end



some_program.rb
---------------
require 'my_lib.rb'

def greet()
  puts "Hi"
end

Lib.greet
Lib.show(10)

greet


-- 
Posted via http://www.ruby-forum.com/.