Ruby/Language Basics/require

Материал из Wiki.crossplatform.ru

Версия от 17:59, 13 сентября 2010; ViGOur (Обсуждение | вклад)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

A commonly used shortcut uses arrays to quickly load a collection of libraries at once.

%w{file1 file2 file3 file4 file5}.each { |l| require l }



Nested Inclusions

//assume a.rb contains the following:
require "b"
//And b.rb contains the following:
require "c"
//And c.rb contains the following:
def example
  puts "Hello!"
end
//And d.rb contains the following:
require "a"



put this code in a file called string_extensions.rb:

class String
  def vowels
    self.scan(/[aeiou]/i)
  end
end
# And put this code in a file called vowel_test.rb:
require "string_extensions"
puts "This is a test".vowels.join("-")



Test require command

//Put this in a.rb:
puts "Hello from a.rb"
//And put this in a file called b.rb:
require "a"
puts "Hello from b.rb"
require "a"
puts "Hello again from b.rb"
//Run with ruby b.rb