Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > require not working correctly?

Reply
Thread Tools

require not working correctly?

 
 
Adlai
Guest
Posts: n/a
 
      05-20-2009
I'm just starting out in Ruby, and I used the one-click installer. The
version on my computer seems to be Ruby 1.8.6-27.

I'm working through some of the examples in Why's Poignant Guide, and
things seem to have broken down here:

File wordlist.rb contains:
puts 'Wordlist got required'
code_words = {
'starmonkeys' => 'Phil and Pete, those prickly chancellors of the
New Reich',
'catapult' => 'chucky go-go', 'firebomb' => 'Heat-Assisted
Living',
'Nigeria' => "Ny and Jerry's Dry Cleaning (with Donuts)",
'Put the kabosh on' => 'Put the cable box on'
}

File script.rb contains:
require 'wordlist'
# Get evil idea and swap in code words
puts "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!(real, code)
end
# Save the jibberish to a new file
puts "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File:pen("idea-" + idea_name + ".txt", "w") do |f|
f << idea
end

This is basically copied out of the Poignant Guide, with the addition
of the puts method at the start of wordlist.rb.

Both are in the same directory. I navigate to that directory in cmd,
and then type ruby script.rb

It prints Wordlist got required, then prompts for an evil idea, and
after I hit enter, gives the following error:
script.rb:5: undefined local variable or method `code_words' for
main:Object (NameError)

What am I doing wrong?

Thanks,

- Adlai
 
Reply With Quote
 
 
 
 
Roger Pack
Guest
Posts: n/a
 
      05-20-2009
> File script.rb contains:
> require 'wordlist'



> It prints Wordlist got required, then prompts for an evil idea, and
> after I hit enter, gives the following error:
> script.rb:5: undefined local variable or method `code_words' for
> main:Object (NameError)
>
> What am I doing wrong?


The scripts operate each in its own "scope" so setting a variable in one
doesn't set it in the other. Kind of confusing at first, but at least
you don't have to worry about leaked variables from one script to the
next.
To avoid it save it to a global like
$code_words
or put it all in one .rb file.
GL!
-=r
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Adlai
Guest
Posts: n/a
 
      05-20-2009
On May 20, 11:44*pm, Roger Pack <rogerpack2...@gmail.com> wrote:
> > File script.rb contains:
> > require 'wordlist'
> > It prints Wordlist got required, then prompts for an evil idea, and
> > after I hit enter, gives the following error:
> > script.rb:5: undefined local variable or method `code_words' for
> > main:Object (NameError)

>
> > What am I doing wrong?

>
> The scripts operate each in its own "scope" so setting a variable in one
> doesn't set it in the other. *Kind of confusing at first, but at least
> you don't have to worry about leaked variables from one script to the
> next.
> To avoid it save it to a global like
> $code_words


I figured out this trick myself, but globals could cause problems in
larger projects with name collisions...

> or put it all in one .rb file.
> GL!
> -=r
> --
> Posted viahttp://www.ruby-forum.com/.


Thanks for your help. Just wanted to make sure that I wasn't making
some newbie mistake.

- Adlai
 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      05-20-2009
> I figured out this trick myself, but globals could cause problems in
> larger projects with name collisions...


The only way to share things across scopes is constants or globals--that
I know of


So you could rename it CODE_WORDS and that would work.
-=r
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      05-20-2009
Roger Pack wrote:
>> I figured out this trick myself, but globals could cause problems in
>> larger projects with name collisions...

>
> The only way to share things across scopes is constants or globals--that
> I know of


You can also write your own #load method, and use the return value to
access things defined in the loaded file. It's actually quite easy to do
this. Here's the approach I use:

http://redshift.sourceforge.net/script/

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Adlai
Guest
Posts: n/a
 
      05-21-2009
On May 21, 12:39*am, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> Roger Pack wrote:
> >> I figured out this trick myself, but globals could cause problems in
> >> larger projects with name collisions...

>
> > The only way to share things across scopes is constants or globals--that
> > I know of

>
> You can also write your own #load method, and use the return value to
> access things defined in the loaded file. It's actually quite easy to do
> this. Here's the approach I use:
>
> http://redshift.sourceforge.net/script/
>
> --
> * * * *vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Thank you Joel. I'll let you know how I like your Script thingy after
I play around with it later today.

- Adlai
 
Reply With Quote
 
Marc Heiler
Guest
Posts: n/a
 
      05-23-2009
Does this work with local variables too?

For example, lets say I have a file called:

foobar.rb

Inside it we could have a class

class Foo
def test
puts 'hi from class Foo'
end
end

foo_object = Foo.new


Now, I would like to use this variable
foo_object
in another ruby file, or context.

But as far as I know without eval this was not possible.

Is this somehow possible with 'script'? (My brain is a bit confused
right now.)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      05-23-2009
Marc Heiler wrote:
> Does this work with local variables too?
>
> For example, lets say I have a file called:
>
> foobar.rb
>
> Inside it we could have a class
>
> class Foo
> def test
> puts 'hi from class Foo'
> end
> end
>
> foo_object = Foo.new
>
>
> Now, I would like to use this variable
> foo_object
> in another ruby file, or context.
>
> But as far as I know without eval this was not possible.
>
> Is this somehow possible with 'script'? (My brain is a bit confused
> right now.)


With 'script', you can only export constants and methods. So you could
do this:

FOO_OBJECT = Foo.new

and then your main file can do this:

my_script = Script.load("foobar.rb")
p my_script::FOO_OBJECT

Even though you are defining a constant, it is accessible only via the
object assigned to my_script (the object is in fact a module), so you
don't have to worry about namespace pollution.

HTH...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with gems (require not working) Tomas Korcak Ruby 8 12-22-2010 11:47 PM
Require a module, but not fail if not found Mr. Nonsense Perl Misc 0 07-20-2009 09:17 PM
require not working on server Jeff Pritchard Ruby 4 08-19-2008 01:07 PM
Form Require Hidden Input Not Working tea-mad brit HTML 5 01-17-2006 11:24 PM
require 'z' not working Brian Takita Ruby 0 07-07-2005 09:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57