From: Daniel Schierbeck Date: 2005-12-09T00:03:14+09:00 Subject: Re: They say I write Ruby like Perl On Wednesday 07 December 2005 09:13 pm, Steve Litt wrote: > 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 > class Node < Object No need to write `< Object', since all objects inherit from Object > super() You don't need the parantheses > @parent = nil > @prevsibling = nil > @nextsibling = nil > @firstchild = nil > @lastchild = nil Variables are created when they're first referenced, and are `nil' by default, so these lines make no difference. > attr_reader :name, :type, :value > attr_writer :name, :type, :value This will do the same: attr_accessor :name, :type, :value > def setAttributes(attribs) @attribs = attribs; end > def getAttributes() return @attribs; end This will do the same: attr_accessor :attribs > def hasAttributes() return @attribs != nil; end def has_attribs? # Hash#empty? returns true if the hash has no elements not @attribs.empty? end > def setAttribute(key, value) @attribs[key] = value; end > def getAttribute(key) return @attribs[key]; end def []=(key, value) @attribs[key] = value end def [](key) @attribs[key] end > def hasAttribute(key) return @attribs[key] != nil; end def has_attrib?(key) @attribs.has_key? key end That's just some of the things though. Cheers, Daniel