开发者

Ruby koans triangle.rb require error

I'm doing the Ruby Koans tutorial, using notepad++.

The about_triangle_project.rb can't seem to load the triangle.rb file.

no such file to load -- triangle.rb <LoadError>
from <internal:lib/rubygems/custom_require>:29:in 'require'
from about_triangle_project.rb:4: in '<main>'

However I don't think I've altered the files. (I tried to fix it but always undid these when they didn't work)... Here's the code in about_triangle_project.rb

require File.expand_path(File.dirname(__FILE__) + '/edgecase')

require 'triangle.rb' # this is line 4

class AboutTriangleProject < EdgeCase::Koan
  def test_equilateral_triangles_have_equal_sides
    assert_equal :equilateral, triangle(2, 2, 2)
    assert_equal :equilateral, triangle(10, 10, 10)
  end
(etc)

I have tried require 'triangle', that didn't work. I tried using an absolute pathname, that didn't work.

and the triangle.rb file is in the same directory, unaltered, with comments and just this:

def triang开发者_StackOverflow社区le(a,b,c)
end
class TriangleError < StandardError
end

The triangle.rb file does exist in the same directory, so why can't it be found? I hope I'm not missing something glaringly obvious!


It appears that on Windows, adding the current directory to the load path doesn't quite work right. Substituting require 'triangle.rb' for require_relative 'triangle.rb' should work, but is a bit of a hack. I don't use Windows, so I'm not sure what the proper solution would be.


I'd definitely look into obtaining a version of Sublime Text Editor, it makes things much cleaner and you can actually open folders in it.

And it looks like your pathing is wrong, I would say make sure that the address of triangle.rb is correct in your code.

Mine looks something more like this

require File.expand_path(File.dirname(FILE) + '/neo')

#You need to write the triangle method in the file 'triangle.rb'

require './triangle'


I'm learning ruby with the Koans right now and found this issue too.

So what was happening to us is that the current working directory (cwd) where the code is ran from affects ruby's require method. You will need to change where the cwd is to the folder of all the koans with cd .\.ruby\koans\ for example. Or, as Ben Langfeld answered, require_relative 'triangle' is a good and easy alternative.

For more info, I'd suggest checking out this What is the difference between require_relative and require in Ruby? thread. My takeaway from this was that require is better used for installed gems and libraries while require_relative is better for code written by you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜