[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

undefined method `+' for nil:NilClass (NoMeth

Mahen Surinam

9/11/2007 3:32:00 PM

Dear All,

Am trying to create a character generator in RUBY. it will create
characters from a-z in the folowing way:

a
b
c
z
aa
ab
ac

and so on. sor here is my first code:


Array = ['a', 'b', 'c' ,'d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

$Text = "";
$OldText = "";
$OldText2 = ""
$OldText3 = ""

def Join()
$Text = $Oldtext3 + $OldText2 + $OldText + $JOB
end

while (1)
Array.each do |$JOB|
Join()
puts $Text
end
end



Eventually, $oldtext2-3 will be filled in later, bt am geting this
error:

C:/WINDOWS/thread.rb:10:in `Join': undefined method `+' for nil:NilClass
(NoMeth
odError)
from C:/WINDOWS/thread.rb:15
from C:/WINDOWS/thread.rb:14:in `each'
from C:/WINDOWS/thread.rb:14

C:\WINDOWS>





Could someone please shed some light
--
Posted via http://www.ruby-....

7 Answers

Bertram Scharpf

9/11/2007 4:10:00 PM

0

Hi,


Am Mittwoch, 12. Sep 2007, 00:32:22 +0900 schrieb Mahen Surinam:
> my first code:
>
> Array = ['a', 'b', 'c' ,'d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
> 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Array is a constant already defined (== [].class). I wonder
why you get no warning message. Say

char_array = ("a".."z").to_a

> $Text = "";
> $OldText = "";
> $OldText2 = ""
> $OldText3 = ""
>
> def Join()
> $Text = $Oldtext3 + $OldText2 + $OldText + $JOB
> end

Using globals is not recommended. Although, try something
like

def join_them
$JOB ||= ""
$Text = $Oldtext3 + $OldText2 + $OldText + $JOB
end

Probably you meant $Text, not $JOB.

> C:\WINDOWS>

:-(


Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Gaspard Bucher

9/11/2007 5:30:00 PM

0

Here's a conversion function that does what you want. It was a little
tricky to have 'aa' after 'z' instead of 'ba' :

def convert(v)
res = ''
while(v >= 0)
res = (?a + (v % 26)).chr + res
v = (v/26) - 1
end
res
end

# test
puts((0..800).to_a.map{ |v| convert(v) }.join(' '))

Rob

9/12/2007 12:05:00 AM

0

I have corrected the class code,

# This version includes the enumerable module, gaining access to
collect .inject .each_with_index et al.
class Generator
include Enumerable

def initialize(start="a", limit=52)
@limit = limit
@start = start
end

def each
# Dup here otherwise another call to .each later on starts from
where this left off
char = @start.dup

@limit.times do
# And dup here otherwise the values returned will point to
the same location
# and .collect would return ["ba", "ba", "ba", ... "ba"] or
whatever the last
# character sequence was.
yield char.dup
char.succ!
end
end
end

gen = Generator.new

gen.each { |char| puts char }

# comma separated string
puts gen.inject { |memo, s| "#{memo},#{s}" }

# returns array of codes
puts gen.collect

-- Rob

Rob wrote:
> The succ! operator for String class should do the trick
>
> char = "a"
> 52.times do
> puts char
> char.succ!
> end
>
>
> ** Alternatively as a class
>
> class Generator
> def initialize(start="a", limit=52)
> @limit = limit
> @start = start
> end
>
> def each
> char = @start
> @limit.times do
> yield char
> char.succ!
> end
> end
> end
>
> gen = Generator.new
>
> gen.each { |code| puts code }
>
> -- Rob
>
> Mahen Surinam wrote:
>> Dear All,
>>
>> Am trying to create a character generator in RUBY. it will create
>> characters from a-z in the folowing way:
>>
>> a
>> b
>> c
>> z
>> aa
>> ab
>> ac
>>
>> and so on. sor here is my first code:
>>
>>
>> Array = ['a', 'b', 'c' ,'d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
>> 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>
>> $Text = "";
>> $OldText = "";
>> $OldText2 = ""
>> $OldText3 = ""
>>
>> def Join()
>> $Text = $Oldtext3 + $OldText2 + $OldText + $JOB
>> end
>>
>> while (1)
>> Array.each do |$JOB|
>> Join()
>> puts $Text
>> end
>> end
>>
>>
>>
>> Eventually, $oldtext2-3 will be filled in later, bt am geting this
>> error:
>>
>> C:/WINDOWS/thread.rb:10:in `Join': undefined method `+' for nil:NilClass
>> (NoMeth
>> odError)
>> from C:/WINDOWS/thread.rb:15
>> from C:/WINDOWS/thread.rb:14:in `each'
>> from C:/WINDOWS/thread.rb:14
>>
>> C:\WINDOWS>
>>
>>
>>
>>
>>
>> Could someone please shed some light
>>
>
>
>
> __________ NOD32 2521 (20070911) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://ww...
>
>
>
> __________ NOD32 2521 (20070911) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://ww...
>

Morton Goldberg

9/12/2007 5:40:00 AM

0

On Sep 11, 2007, at 8:04 PM, Rob wrote:

> I have corrected the class code,
>
> # This version includes the enumerable module, gaining access
> to .collect .inject .each_with_index et al.
> class Generator
> include Enumerable
>
> def initialize(start="a", limit=52)
> @limit = limit
> @start = start
> end
>
> def each
> # Dup here otherwise another call to .each later on starts
> from where this left off
> char = @start.dup @limit.times do
> # And dup here otherwise the values returned will point
> to the same location
> # and .collect would return ["ba", "ba", "ba", ... "ba"]
> or whatever the last
> # character sequence was.
> yield char.dup
> char.succ!
> end
> end
> end
>
> gen = Generator.new
>
> gen.each { |char| puts char }
>
> # comma separated string
> puts gen.inject { |memo, s| "#{memo},#{s}" }
>
> # returns array of codes
> puts gen.collect

It seems to me that you are reinventing the wheel -- in this case
'wheel' being the built-in class Range. Why not do it like this?

('a'..'az').each { |chr| puts chr }

('a'..'az').to_a.join(', ') # => "a, b, c, d, e, f, g, h, i, j, k, l,
m, n, o, p, q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af, ag,
ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at, au, av, aw, ax,
ay, az"

('a'..'az').to_a # => ["a", "b", "c", "d", "e", "f", "g", "h", "i",
"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
"x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai",
"aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at",
"au", "av", "aw", "ax", "ay", "az"]

Regards, Morton

Gaspard Bucher

9/12/2007 6:21:00 AM

0

This is fine to get the full list but you cannot get the 1200th item
without expanding a list.
This doesn't really look like a wheel to me:
def convert(v)
res = ''
while(v >= 0)
res = (?a + (v % 26)).chr + res
v = (v/26) - 1
end
res
end

2007/9/12, Morton Goldberg <m_goldberg@ameritech.net>:
> On Sep 11, 2007, at 8:04 PM, Rob wrote:
>
> > I have corrected the class code,
> >
> > # This version includes the enumerable module, gaining access
> > to .collect .inject .each_with_index et al.
> > class Generator
> > include Enumerable
> >
> > def initialize(start="a", limit=52)
> > @limit = limit
> > @start = start
> > end
> >
> > def each
> > # Dup here otherwise another call to .each later on starts
> > from where this left off
> > char = @start.dup @limit.times do
> > # And dup here otherwise the values returned will point
> > to the same location
> > # and .collect would return ["ba", "ba", "ba", ... "ba"]
> > or whatever the last
> > # character sequence was.
> > yield char.dup
> > char.succ!
> > end
> > end
> > end
> >
> > gen = Generator.new
> >
> > gen.each { |char| puts char }
> >
> > # comma separated string
> > puts gen.inject { |memo, s| "#{memo},#{s}" }
> >
> > # returns array of codes
> > puts gen.collect
>
> It seems to me that you are reinventing the wheel -- in this case
> 'wheel' being the built-in class Range. Why not do it like this?
>
> ('a'..'az').each { |chr| puts chr }
>
> ('a'..'az').to_a.join(', ') # => "a, b, c, d, e, f, g, h, i, j, k, l,
> m, n, o, p, q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af, ag,
> ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at, au, av, aw, ax,
> ay, az"
>
> ('a'..'az').to_a # => ["a", "b", "c", "d", "e", "f", "g", "h", "i",
> "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
> "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai",
> "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at",
> "au", "av", "aw", "ax", "ay", "az"]
>
> Regards, Morton
>
>

Mahen Surinam

9/13/2007 5:07:00 PM

0

Gareth Adams wrote:
> Mahen Surinam <neoanderson12 <at> gmail.com> writes:
>>
>> [.. snip ..]
>
> The error you were getting is because $Oldtext3 is not the same as
> $OldText3
>
> However, all of the previous replies highlight other issues which you
> should
> address in your code - the biggest of which (in my opinion) is using
> globals for
> a task which only ever stays in one scope.
>
> Gareth





Am using this piece of code. very effective. I;ve tweaked it a little.
By the way. I've kept it running for some minute now and I saw in
taksmanager that Ruby was consuming more thatn 200 megabytes, then
sharply decreases to 70 mega then again to 200+ . I also saw that my
pagefile usage kept on increasing. is this a issues with the garbase
collector? am using ruby 1.8.6
--
Posted via http://www.ruby-....

Mahen Surinam

9/13/2007 5:08:00 PM

0

Gareth Adams wrote:
> Mahen Surinam <neoanderson12 <at> gmail.com> writes:
>>
>> [.. snip ..]
>
> The error you were getting is because $Oldtext3 is not the same as
> $OldText3
>
> However, all of the previous replies highlight other issues which you
> should
> address in your code - the biggest of which (in my opinion) is using
> globals for
> a task which only ever stays in one scope.
>
> Gareth

I meant this code:


array = []
char = "a"

0.upto(25) do
array << char
array << array[0] + char
char = char.next
end
--
Posted via http://www.ruby-....