1

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!

3
  • If you get the error class/module name must be CONSTANT (SyntaxError), then there is a problem with your code, not with require. Commented Jan 2, 2015 at 17:27
  • From the last paragraph of your question, it is clear that the require is working but there is an error on line 1 of the file you are requiring. Are you sure you posted the exact contents of that file here, with no typos? Commented Jan 2, 2015 at 17:27
  • Thank you guys so much . I found the issue, I didn't capitalize "class string" it should be " class String" with a capitalised "S" Commented Jan 2, 2015 at 17:31

2 Answers 2

3

You can use require_relative instead:

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

Or even require './string_extensions'.

Sign up to request clarification or add additional context in comments.

Comments

2

Use:

ruby -I. vowels_test.rb 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.