Brian Warner
2011-Jan-20 19:32 UTC
[rspec-users] ''require''ing files from other directories
I''m going through The RSpec Book and am currently working with the Mastermind game example. But that''s besides the point. The way my directories are setup is like so: /mastermind/features/step_definitions... /mastermind/lib/codebreaker/... I have a file in step_definitions that''s giving me an error for an uninitialized constant. My guess is I need to ''require'' the file where that class is defined. Said files is located in /lib/codebreaker. I am very new to Ruby so this is probably a really simple question but how do I ''require'' a file in codebreaker from a file inside step_definitions? -- Posted via http://www.ruby-forum.com/.
Usually you can do some relative path requires, you can do it on your spec/spec_helper.rb require File.expand_path("../../lib/codebreaker", __FILE__) or, another common setup is to add the "lib" into require path list, and them just require the file: $: << File.expand_path("../../lib", __FILE__) require "codebreaker" another hint: you should require only main file of lib (codebreaker), and this file in lib should require the others (also, if you have some time, research about autoload, will be useful too ;)) --- Wilker L?cio http://about.me/wilkerlucio/bio Kajabi Consultant +55 81 82556600 On Thu, Jan 20, 2011 at 4:32 PM, Brian Warner <lists at ruby-forum.com> wrote:> I''m going through The RSpec Book and am currently working with the > Mastermind game example. > > But that''s besides the point. > > The way my directories are setup is like so: > > /mastermind/features/step_definitions... > /mastermind/lib/codebreaker/... > > I have a file in step_definitions that''s giving me an error for an > uninitialized constant. My guess is I need to ''require'' the file where > that class is defined. Said files is located in /lib/codebreaker. > > I am very new to Ruby so this is probably a really simple question but > how do I ''require'' a file in codebreaker from a file inside > step_definitions? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110120/2d7e9363/attachment.html>
Matt Wynne
2011-Jan-21 00:58 UTC
[rspec-users] ''require''ing files from other directories
On 20 Jan 2011, at 19:32, Brian Warner wrote:> I''m going through The RSpec Book and am currently working with the > Mastermind game example. > > But that''s besides the point. > > The way my directories are setup is like so: > > /mastermind/features/step_definitions... > /mastermind/lib/codebreaker/... > > I have a file in step_definitions that''s giving me an error for an > uninitialized constant. My guess is I need to ''require'' the file where > that class is defined. Said files is located in /lib/codebreaker. > > I am very new to Ruby so this is probably a really simple question but > how do I ''require'' a file in codebreaker from a file inside > step_definitions?You could try asking on the RSpec Book''s own forum: http://forums.pragprog.com/forums/95 This question is really more Cucumber related so you could also ask on the cukes google group. Your guess is right. The best place to put your require statement is in features/support/env.rb I''m surprised that the book doesn''t tell you to do that - did you maybe miss a step? cheers, Matt matt at mattwynne.net 07974 430184
Brian Warner
2011-Jan-21 16:22 UTC
[rspec-users] ''require''ing files from other directories
Matt Wynne wrote in post #976412:> On 20 Jan 2011, at 19:32, Brian Warner wrote: > >> I have a file in step_definitions that''s giving me an error for an >> uninitialized constant. My guess is I need to ''require'' the file where >> that class is defined. Said files is located in /lib/codebreaker. >> >> I am very new to Ruby so this is probably a really simple question but >> how do I ''require'' a file in codebreaker from a file inside >> step_definitions? > > You could try asking on the RSpec Book''s own forum: > http://forums.pragprog.com/forums/95 > > This question is really more Cucumber related so you could also ask on > the cukes google group. > > Your guess is right. The best place to put your require statement is in > features/support/env.rb > > I''m surprised that the book doesn''t tell you to do that - did you maybe > miss a step? > > cheers, > Matt > > matt at mattwynne.net > 07974 430184The require statement that the book says to put in env.rb is: $: << File.join(File.dirname(__FILE__), "/../lib/") require ''codebreaker'' I ended up getting it to work after a co-worker pointed out that I need to add "./lib" to some thing in ruby''s list of directories to look in. I don''t remember what terminology he used but he did it through the terminal. The require statement I ended up using is require ''codebreaker/game'' -- Posted via http://www.ruby-forum.com/.
Matt Wynne
2011-Jan-21 21:15 UTC
[rspec-users] ''require''ing files from other directories
On 21 Jan 2011, at 16:22, Brian Warner wrote:> Matt Wynne wrote in post #976412: >> On 20 Jan 2011, at 19:32, Brian Warner wrote: >> >>> I have a file in step_definitions that''s giving me an error for an >>> uninitialized constant. My guess is I need to ''require'' the file where >>> that class is defined. Said files is located in /lib/codebreaker. >>> >>> I am very new to Ruby so this is probably a really simple question but >>> how do I ''require'' a file in codebreaker from a file inside >>> step_definitions? >> >> You could try asking on the RSpec Book''s own forum: >> http://forums.pragprog.com/forums/95 >> >> This question is really more Cucumber related so you could also ask on >> the cukes google group. >> >> Your guess is right. The best place to put your require statement is in >> features/support/env.rb >> >> I''m surprised that the book doesn''t tell you to do that - did you maybe >> miss a step? >> >> cheers, >> Matt >> >> matt at mattwynne.net >> 07974 430184 > > The require statement that the book says to put in env.rb is: > > $: << File.join(File.dirname(__FILE__), "/../lib/") > require ''codebreaker'' > > I ended up getting it to work after a co-worker pointed out that I need > to add "./lib" to some thing in ruby''s list of directories to look in. I > don''t remember what terminology he used but he did it through the > terminal.He probably called it the ''load path''. That''s what the $: means in that snippet - it''s an Array of paths where Ruby will search for files when you call require. In the code above, you''re pushing the lib directory onto the load path.> The require statement I ended up using is > > require ''codebreaker/game''Maybe the lib/codebreaker file was supposed to do that? cheers, Matt matt at mattwynne.net 07974 430184
Andrew Premdas
2011-Jan-21 23:17 UTC
[rspec-users] ''require''ing files from other directories
On 21 January 2011 21:15, Matt Wynne <matt at mattwynne.net> wrote:> > On 21 Jan 2011, at 16:22, Brian Warner wrote: > > > Matt Wynne wrote in post #976412: > >> On 20 Jan 2011, at 19:32, Brian Warner wrote: > >> > >>> I have a file in step_definitions that''s giving me an error for an > >>> uninitialized constant. My guess is I need to ''require'' the file where > >>> that class is defined. Said files is located in /lib/codebreaker. > >>> > >>> I am very new to Ruby so this is probably a really simple question but > >>> how do I ''require'' a file in codebreaker from a file inside > >>> step_definitions? > >> > >> You could try asking on the RSpec Book''s own forum: > >> http://forums.pragprog.com/forums/95 > >> > >> This question is really more Cucumber related so you could also ask on > >> the cukes google group. > >> > >> Your guess is right. The best place to put your require statement is in > >> features/support/env.rb > >> > >> I''m surprised that the book doesn''t tell you to do that - did you maybe > >> miss a step? > >> > >> cheers, > >> Matt > >> > >> matt at mattwynne.net > >> 07974 430184 > > > > The require statement that the book says to put in env.rb is: > > > > $: << File.join(File.dirname(__FILE__), "/../lib/") > > require ''codebreaker'' > > > > I ended up getting it to work after a co-worker pointed out that I need > > to add "./lib" to some thing in ruby''s list of directories to look in. I > > don''t remember what terminology he used but he did it through the > > terminal. > > He probably called it the ''load path''. That''s what the $: means in that > snippet - it''s an Array of paths where Ruby will search for files when you > call require. In the code above, you''re pushing the lib directory onto the > load path. > > > The require statement I ended up using is > > > > require ''codebreaker/game'' > > Maybe the lib/codebreaker file was supposed to do that? > > It should, mine currently hasrequire ''codebreaker/game'' require ''codebreaker/marker'' All best Andrew> > cheers, > Matt > > matt at mattwynne.net > 07974 430184 > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- ------------------------ Andrew Premdas blog.andrew.premdas.org -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110121/449b842e/attachment.html>