Undefined method `+' for nil:NilClass (NoMeth

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

I don’t see $JOB being defined anywhere.

On 9/11/07, Mahen S. [email protected] wrote:

aa
$OldText = “”;
puts $Text
odError)
Could someone please shed some light

Posted via http://www.ruby-forum.com/.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

I would avoid the globals as much as possible. You also need to rename
Array to be something other than the class name of array. Try something
like:

my_array.each do |char|
join(char)
end

def join(char)
$TEXT = $Oldtext3 + $Oldtext2 + $Oldtext + char
end

This is going to need more work to do what you want though.

On 9/11/07, Glen H. [email protected] wrote:

a
Array = [‘a’, ‘b’, ‘c’ ,‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’,

error:


“Hey brother Christian with your high and mighty errand, Your actions
speak so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

Hi,

Am Mittwoch, 12. Sep 2007, 00:32:22 +0900 schrieb Mahen S.:

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>

:frowning:

Bertram

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

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

On 9/11/07, Glen H. [email protected] wrote:

$TEXT = $Oldtext3 + $Oldtext2 + $Oldtext + char

On 9/11/07, Mahen S. [email protected] wrote:

aa
$OldText = “”;
puts $Text
(NoMeth

-Greg Graffin (Bad Religion)

Sorry my email is acting up. This should get what you want (I think)

array = []
char = “a”

0.upto(25) do
array << char
array << array[0] + char
char = char.next
end

That is assuming you wanted to just get a…z and aa…az if you want the
rest of the two letter combos a little tweaking should yield that.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

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

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 G. [email protected]:

Mahen S. <neoanderson12 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

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

end
yield char.dup
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

Gareth A. wrote:

Mahen S. <neoanderson12 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

Gareth A. wrote:

Mahen S. <neoanderson12 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

On 9/13/07, Mahen S. [email protected] wrote:

should
char = “a”

0.upto(25) do
array << char
array << array[0] + char
char = char.next
end

Posted via http://www.ruby-forum.com/.

I’m not sure what would be causing that. When I run just that code from
in
irb I don’t see any huge spikes in memory usage.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs