Hello
Im doing a simple test. I have 2 files:
mouse.rb
···
---------
my_string = 'blah'
rabbit.rb
---------
require 'foo'
print my_string
eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for main:Object (NameError)
Im simplified my example down to this; and I cant see what im doing wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies? http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
If you mean your are requiring 'mouse' in rabbit, then my_string would be considered a variable local to 'mouse'
-Justin
eoghan wrote:
···
Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string = 'blah'
rabbit.rb
---------
require 'foo'
print my_string
eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for main:Object (NameError)
Im simplified my example down to this; and I cant see what im doing wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies? http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
Unlike in php or c require and load do not simply replace the
statement with the file, but are carefull not to introduce any local
variables. Try the following.
mouse.rb
···
On 19/10/05, eoghan <ruby@redry.net> wrote:
Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string = 'blah'
rabbit.rb
---------
require 'foo'
print my_string
eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for
main:Object (NameError)
Im simplified my example down to this; and I cant see what im doing
wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies? http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
---
module AnimalConstants
MOUSE_NAME = "Mickey Mouse"
end
rabbit.rb
---s
require 'mouse'
p AnimalConstants::MOUSE_NAME
Constants and Globals are imported. (Note that you do not need to
structure your constants using a module, it was just to show this
effekt.)
Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string = 'blah'
rabbit.rb
---------
require 'foo'
print my_string
eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for
main:Object (NameError)
Im simplified my example down to this; and I cant see what im doing
wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies? http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
Unlike in php or c require and load do not simply replace the
statement with the file, but are carefull not to introduce any local
variables. Try the following.
mouse.rb
---
module AnimalConstants
MOUSE_NAME = "Mickey Mouse"
end
rabbit.rb
---s
require 'mouse'
p AnimalConstants::MOUSE_NAME
Constants and Globals are imported. (Note that you do not need to
structure your constants using a module, it was just to show this
effekt.)
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'
}
i assume this should be saved to a file too...?
require 'wordlist'
# Get evil idea and swap in code words
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end
# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end
This doesnt work either. For me at least. Any ideas?
Eoghan
this is a known bug in _why's tutorial. It would be a good idea if it
were fixed in the tutorial, as this question comes up from time to
time on this mailing list. Change code_words to CODE_WORDS and it
works.
regards,
Brian
···
On 19/10/05, eoghan <ruby@redry.net> wrote:
Brian Schröder wrote:
> On 19/10/05, eoghan <ruby@redry.net> wrote:
>> Hello
>> Im doing a simple test. I have 2 files:
>> mouse.rb
>> ---------
>> my_string = 'blah'
>>
>> rabbit.rb
>> ---------
>> require 'foo'
>>
>> print my_string
>>
>> eoghanj$ /opt/local/bin/ruby bar.rb
>> bar.rb:3: undefined local variable or method `my_string' for
>> main:Object (NameError)
>>
>> Im simplified my example down to this; and I cant see what im doing
>> wrong... hope its not too stupid.
>> I read this part about irb restrictions, but im not sure it applies?
>> http://www.rubycentral.com/book/irb.html
>> Thanks
>> Eoghan
>>
>>
>
> Unlike in php or c require and load do not simply replace the
> statement with the file, but are carefull not to introduce any local
> variables. Try the following.
>
> mouse.rb
> ---
> module AnimalConstants
> MOUSE_NAME = "Mickey Mouse"
> end
>
> rabbit.rb
> ---s
> require 'mouse'
>
> p AnimalConstants::MOUSE_NAME
>
> Constants and Globals are imported. (Note that you do not need to
> structure your constants using a module, it was just to show this
> effekt.)
>
> regards,
>
> Brian
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'
}
i assume this should be saved to a file too...?
require 'wordlist'
# Get evil idea and swap in code words
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end
# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end
This doesnt work either. For me at least. Any ideas?
Eoghan
Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string = 'blah'
rabbit.rb
---------
require 'foo'
print my_string
eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for
main:Object (NameError)
Im simplified my example down to this; and I cant see what im doing
wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies? http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
Unlike in php or c require and load do not simply replace the
statement with the file, but are carefull not to introduce any local
variables. Try the following.
mouse.rb
---
module AnimalConstants
MOUSE_NAME = "Mickey Mouse"
end
rabbit.rb
---s
require 'mouse'
p AnimalConstants::MOUSE_NAME
Constants and Globals are imported. (Note that you do not need to
structure your constants using a module, it was just to show this
effekt.)
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'
}
i assume this should be saved to a file too...?
require 'wordlist'
# Get evil idea and swap in code words
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end
# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end
This doesnt work either. For me at least. Any ideas?
Eoghan
Hello Eoghan,
this is a known bug in _why's tutorial. It would be a good idea if it
were fixed in the tutorial, as this question comes up from time to
time on this mailing list. Change code_words to CODE_WORDS and it
works.
It might be instructive to point out that the reason changing code_words to CODE_WORDS works is because variables beginning with a capital letter are constants and constants are accessible when you do a require.
-Justin Collins
eoghan wrote:
···
Brian Schröder wrote:
On 19/10/05, eoghan <ruby@redry.net> wrote:
Brian Schröder wrote:
On 19/10/05, eoghan <ruby@redry.net> wrote:
Hello
Im doing a simple test. I have 2 files:
mouse.rb
---------
my_string = 'blah'
rabbit.rb
---------
require 'foo'
print my_string
eoghanj$ /opt/local/bin/ruby bar.rb
bar.rb:3: undefined local variable or method `my_string' for
main:Object (NameError)
Im simplified my example down to this; and I cant see what im doing
wrong... hope its not too stupid.
I read this part about irb restrictions, but im not sure it applies? http://www.rubycentral.com/book/irb.html
Thanks
Eoghan
Unlike in php or c require and load do not simply replace the
statement with the file, but are carefull not to introduce any local
variables. Try the following.
mouse.rb
---
module AnimalConstants
MOUSE_NAME = "Mickey Mouse"
end
rabbit.rb
---s
require 'mouse'
p AnimalConstants::MOUSE_NAME
Constants and Globals are imported. (Note that you do not need to
structure your constants using a module, it was just to show this
effekt.)
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'
}
i assume this should be saved to a file too...?
require 'wordlist'
# Get evil idea and swap in code words
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end
# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::open( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end
This doesnt work either. For me at least. Any ideas?
Eoghan
Hello Eoghan,
this is a known bug in _why's tutorial. It would be a good idea if it
were fixed in the tutorial, as this question comes up from time to
time on this mailing list. Change code_words to CODE_WORDS and it
works.