gqli
gqli

Reputation: 1045

require not working on ruby 2.0?

I'm doing a test with "require" under ruby 2.0.0p576 (2014-09-19 revision 47628) [x86_64-darwin13.4.0] it doesn't work in many ways. There are two files in ruby directory as shown below:

string_extensions.rb

class String
         def vowels
           self.scan(/[aeiou]/i)
         end
end

vowels_test.rb

require 'string_extensions'
   puts "This is a test".vowels.join('-')

fire up IRB

Snailwalkers-MacBook-Pro:ruby snailwalker$  ruby vowels_test.rb

returs : `require': cannot load such file -- string_extensions (LoadError)

I tried to change require 'string_extensions' to " require_relative 'string_extensions' ; require './string_extensions.rb' . They all didn't work.

both return error : vowels_test.rb:1:in require_relative': /Users/snailwalker/Ruby/string_extensions.rb:1: class/module name must be CONSTANT (SyntaxError)

Your help will be greatly appreciated!

Upvotes: 1

Views: 581

Answers (2)

August
August

Reputation: 12558

You can use require_relative instead:

require_relative 'string_extensions'
puts "This is a test".vowels.join('-')

Or even require './string_extensions'.

Upvotes: 3

Dave Newton
Dave Newton

Reputation: 160170

Use:

ruby -I. vowels_test.rb

Automatic inclusion of the current directory in the load paths was removed in Ruby 2.

Upvotes: 2

Related Questions