undefined method `+' for nil:NilClass (NoMeth

M

Mahen Surinam

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
 
B

Bertram Scharpf

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
 
G

Gaspard Bucher

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(' '))
 
R

Rob

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
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 said:
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://www.eset.com



__________ NOD32 2521 (20070911) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com
 
M

Morton Goldberg

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
 
G

Gaspard Bucher

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 said:
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
 
M

Mahen Surinam

Gareth said:
Mahen Surinam said:
[.. 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
 
M

Mahen Surinam

Gareth said:
Mahen Surinam said:
[.. 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top