Recursive Objects (Arrays) in Ruby

As a disclaimer, I'm new to object-oriented programming and to Ruby; so
I'm not sure if I'm even describing this problem correctly. Here goes:

I have an object called "formula" which has properties .string (the
original formula), .leftSub, .rightSub (left and right subformula of
the original formula) and .characterType (an array identifying each
character in .string as being a particular type - e.g., number, letter,
and so on).

Now, I have the following:

class Formula

def initialize
@string = nil
@leftSub = nil
@rightSub = nil
@characterType = Array.new
end

def string=(newString)
@string = newString
mapCharacter
findLeftRight
end

def mapCharacter
#This method goes through
#each character in .string
#and maps to @characterType
#array.
end

def findLeftRight
#This method finds and
#assigns @leftSub, @rightSub.
end

def wff

testFormula1 = Formula.new
testFormula2 = Formula.new
testFormula1.string = @leftSub
testFormula2.string = @rightSub

#Problem line:
puts testFormula1.characterType[0]

end
end

Here is the main program:

test = Formula.new
test.string = "A&B"
test.wff

Now, Ruby exits with the following complaint:

in `wff': undefined method `characterType' for #<Formula:0xbceb0>
(NameError)

It is as if I am not referring to the element of the array correctly;
but I can't figure out exactly how I should be referring to it. Any
help would be appreciated.

#Problem line:
puts testFormula1.characterType[0]

....

test = Formula.new
test.string = "A&B"
test.wff

Now, Ruby exits with the following complaint:

in `wff': undefined method `characterType' for #<Formula:0xbceb0>
(NameError)

I think the problem is, Ruby is not like C++ where one object
can always read / change the internals of another if they're
of the same class. It looks like Ruby's 'protected' might
produce that C++ behavior but I haven't used it. Pickaxe pg 236
makes it look like it only works with methods and constants,
ie you can't expose the raw variable itself. The easiest way to
make accessors is to make the variable an 'attr'; eg

class Formula
  attr :characterType # add ', true' if you want it writable too

  def initialize
    @string = nil
    @leftSub = nil
    @rightSub = nil
    @characterType = Array.new
  end

  # and I'm guessing something like this to give it C++ behavior
  # (ie what you were trying to do):
  protected characterType, characterType=
  # 'char..Type=' , only if you've specified a writer function
  # for char..Type

....
end

···

On Mon, 13 Sep 2004 20:54:21 -0700, Wandering Mango wrote:

"Wandering Mango" <dropbox@whoever.com> schrieb im Newsbeitrag
news:ci5q1d$a9b@odah37.prod.google.com...

As a disclaimer, I'm new to object-oriented programming and to Ruby; so
I'm not sure if I'm even describing this problem correctly. Here goes:

I have an object called "formula" which has properties .string (the
original formula), .leftSub, .rightSub (left and right subformula of
the original formula) and .characterType (an array identifying each
character in .string as being a particular type - e.g., number, letter,
and so on).

Assaph answered your problem question, but I'd like to raise another
question: this looks like an expression tree - possible as outcome of a
parse stage. It seems to me that the more appropriate storage would be
(besides the original string) a sequence of tokens per node.

Just my 0.02

    robert

"Wandering Mango" <drxpxbxx@wxxoevxer.xoxm> schrieb im Newsbeitrag
news:slrnckegn0.q4u.drxpxbxx@octothorpe.local...

>
> "Wandering Mango" <dropbox@whoever.com> schrieb im Newsbeitrag
> news:ci5q1d$a9b@odah37.prod.google.com...
>> As a disclaimer, I'm new to object-oriented programming and to Ruby;

so

>> I'm not sure if I'm even describing this problem correctly. Here

goes:

>>
>> I have an object called "formula" which has properties .string (the
>> original formula), .leftSub, .rightSub (left and right subformula of
>> the original formula) and .characterType (an array identifying each
>> character in .string as being a particular type - e.g., number,

letter,

>> and so on).
>
> Assaph answered your problem question, but I'd like to raise another
> question: this looks like an expression tree - possible as outcome of

a

> parse stage. It seems to me that the more appropriate storage would

be

> (besides the original string) a sequence of tokens per node.

Thank you. Your analysis is quite perceptive: that is excatly where I
am headed. I'm parsing the original string into substrings, and then
doing the same to them until I get to atomic strings. But if there
is a better way to do this, I'm more than willing to entertain it -
could you tell me a litle bit more about what "a sequence of tokens
per node" is? I googled the phrase, but am not exactly sure what
I am looking for. Thank you.

You should probably use a parser generator such as
http://raa.ruby-lang.org/project/racc/ and
http://raa.ruby-lang.org/project/ruby-yacc/ You'll finde more on the RAA
here: http://raa.ruby-lang.org/search.rhtml?search=parser (look out for
parser generators).

You can find more with Google:
http://www.google.com/search?q="parser+generator"+ruby

If you search for "parsing theory" you'll find quite some resources on the
matter. The classical book about the issue is this:
http://www.amazon.com/exec/obidos/tg/detail/-/0201101947

Kind regards

    robert

···

On 2004-09-14, Robert Klemme <bob.news@gmx.net> wrote: