2005/12/8, Steve Litt <slitt / earthlink.net>:
> 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?
>

Hi Steve,

From the testing code, you have:

def rvExit(checker, level)
		for i in 0...level
			print "\t"
		end
		print checker.value
		print ", Line ", checker.getAttribute("_lineno"), "\n"
end

Which could be written as:

def reverse_exit(checker, level)
  puts "#{"\t" * level}#{checker.value}, Line #{checker[:_line_number]}"
end

No need for the for loop, using print, getAttribute (use []= instead),
or appending a "\n" manually. And with all that saving, you can afford
to write out full method names!

Take a look at Test::Unit for running your tests:
http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/index.html

You could use it to automate all your tests, so you wouldn't need
comments like "Change to b and watch what happens".

Other things:

attr_reader :commentchar, :skipblanks
attr_writer :commentchar, :skipblanks

could be:

attr_accessor :comment_char, :skip_blanks

and:

def skipLine?(line)
		regex_comment = Regexp.new("^\\s*" + @commentchar )
		if @skipblanks and line =~ /^\s*$/
			return true
		elsif @commentchar and regex_comment.match(line)
			return true
		else
			return false
		end
end

could be:

def skip_line?(line)
  return true if @skip_blanks and line =~ /^\s*$/
  return @comment_char and /^\s*#{@comment_char}/.match(line)
end

You get the idea :-)

Good thread, thanks!

hth,
Douglas