undefined method `+' for nil:NilClass (NoMeth

Discussion in 'Ruby' started by Mahen Surinam, Sep 11, 2007.

  1. 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-forum.com/.
     
    Mahen Surinam, Sep 11, 2007
    #1
    1. Advertising

  2. 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-scharpf.de
     
    Bertram Scharpf, Sep 11, 2007
    #2
    1. Advertising

  3. 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(' '))
     
    Gaspard Bucher, Sep 11, 2007
    #3
  4. Mahen Surinam

    Rob Guest

    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://www.eset.com
    >
    >
    >
    > __________ NOD32 2521 (20070911) Information __________
    >
    > This message was checked by NOD32 antivirus system.
    > http://www.eset.com
    >
     
    Rob, Sep 12, 2007
    #4
  5. 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
     
    Morton Goldberg, Sep 12, 2007
    #5
  6. 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 <>:
    > 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, Sep 12, 2007
    #6
  7. 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-forum.com/.
     
    Mahen Surinam, Sep 13, 2007
    #7
  8. 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-forum.com/.
     
    Mahen Surinam, Sep 13, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Petrucci Fabio
    Replies:
    1
    Views:
    104
    Jim Weirich
    Jul 13, 2004
  2. Petrucci Fabio
    Replies:
    1
    Views:
    118
    Jim Weirich
    Jul 13, 2004
  3. Petrucci Fabio
    Replies:
    0
    Views:
    94
    Petrucci Fabio
    Jul 13, 2004
  4. Giacecco
    Replies:
    0
    Views:
    150
    Giacecco
    Jan 27, 2006
  5. Paatsch, Bernd
    Replies:
    1
    Views:
    149
Loading...

Share This Page