D
Daniel Schierbeck
Hi all,
I wrote some hierarchy handling classes in Node.rb
(http://www.troubleshooters.com/projects/Node.rb/index.htm), and I've been
told I write Ruby in Perl style. In future software, what could I do to
write in a more Ruby-like fashion?
Notice that I've substituted your tabs with two spaces and that I've
changed you method names to lower_case instead of camelCase
No need to write `< Object', since all objects inherit from Objectclass Node < Object
You don't need the paranthesessuper()
Variables are created when they're first referenced, and are `nil' by@parent = nil
@prevsibling = nil
@nextsibling = nil
@firstchild = nil
@lastchild = nil
default, so these lines make no difference.
This will do the same:attr_reader :name, :type, :value
attr_writer :name, :type, :value
attr_accessor :name, :type, :value
This will do the same:def setAttributes(attribs) @attribs = attribs; end
def getAttributes() return @attribs; end
attr_accessor :attribs
def has_attribs?def hasAttributes() return @attribs != nil; end
# Hash#empty? returns true if the hash has no elements
not @attribs.empty?
end
def []=(key, value)def setAttribute(key, value) @attribs[key] = value; end
def getAttribute(key) return @attribs[key]; end
@attribs[key] = value
end
def [](key)
@attribs[key]
end
def has_attrib?(key)def hasAttribute(key) return @attribs[key] != nil; end
@attribs.has_key? key
end
That's just some of the things though.
Cheers,
Daniel