Featured post
class - Calling another ruby file that is not a gem -
i want create static ruby class library of function. on vista ruby 1.9.2
my class 1 :
class testclass def say_hello puts "say hello" end end
in testclass.rb file (i assume correct ruby tutorials on classes complete mess putting in single magic (file?) if irb begining , end of thing).
my ruby main()
(yes come java) or program entry or wathever called in ruby :
require 'testclass.rb' puts "start" say_hello
but fails :
c:\ruby_path_with_all_my_classes>ruby classuser.rb <internal:lib/rubygems/custom_require>:29:in `require': no such file load -- testclass.rb (loaderror) <internal:lib/rubygems/custom_require>:29:in `require' classuser.rb:1:in `<main>'
how work? possible call other files in ruby or trapped in 1 file containing classes?
in 99% of cases when computer tells couldn't find thing, because thing isn't there. so, first thing need check whether there is file named testclass.rb
somewhere on filesystem.
in 99% of rest of cases, computer looking in wrong place. (well, actually, computer looking in right place, thing looking in wrong place). require
loads file $load_path
, have make sure directory file testclass.rb
in is on $load_path
.
alternatively, if not want require
file $load_path
rather relative position of file doing require
ing, need use require_relative
.
note, however, code won't work anyway, since say_hello
in instance method of instances of testclass
class, calling on main
object, instance of object
, not testclass
.
note standard naming conventions of ruby files snake_case
, in particular, snake_case
version of primary class/module of file. so, in case, file should named test_class.rb
. also, require
, require_relative
figure out correct file extension themselves, should leave off .rb
. , thirdly, standard ruby coding style 2 spaces indentation, not four.
none of these lead code not working, of course, since purely stylistic, may lead people being unwilling answer questions, since shows don't respect community enough learn basic rules.
- Get link
- X
- Other Apps
Comments
Post a Comment