[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

They say I write Ruby like Perl

Steve Litt

12/8/2005 2:13:00 AM

Hi all,

I wrote some hierarchy handling classes in Node.rb
(http://www.troublesh.../projects/Node.rb...), 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?

Thanks

SteveT

Steve Litt
Author:
* Universal Troubleshooting Process courseware
* Troubleshooting Techniques of the Successful Technologist
* Rapid Learning: Secret Weapon of the Successful Technologist
Webmaster
* Troubleshooters.Com
* http://www.troublesh...


56 Answers

Steve Litt

12/8/2005 2:24:00 AM

0

On Wednesday 07 December 2005 09:13 pm, Steve Litt wrote:
> Hi all,
>
> I wrote some hierarchy handling classes in Node.rb
> (http://www.troublesh.../projects/Node.rb...), 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?
>
> Thanks
>
> SteveT

Whoops, the actual code is here:
http://www.troublesh.../projects/Node.rb/downloads/0...., with
test progarm here:
http://www.troublesh.../projects/Node.rb/downloads/0.02/testnod...
and test data here:
http://www.troublesh.../projects/Node.rb/downloads/0.0...

/testnode_parse < test.otl | less

Thanks

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Logan Capaldo

12/8/2005 2:24:00 AM

0


On Dec 7, 2005, at 9:13 PM, Steve Litt wrote:

> Hi all,
>
> I wrote some hierarchy handling classes in Node.rb
> (http://www.troublesh.../projects/Node.rb...), 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?
>
> Thanks
>
> SteveT
>
> Steve Litt
> Author:
> * Universal Troubleshooting Process courseware
> * Troubleshooting Techniques of the Successful Technologist
> * Rapid Learning: Secret Weapon of the Successful Technologist
> Webmaster
> * Troubleshooters.Com
> * http://www.troublesh...
>

Well with merely skimming your code, I would suggest replacing
camelCase method names with underscore_style ones. Other than that,
it doesn't seem too perlish to me, I don't see a mess of $'s anyway, ;)



dblack

12/8/2005 2:35:00 AM

0

Daniel Berger

12/8/2005 2:38:00 AM

0

Steve Litt wrote:
> On Wednesday 07 December 2005 09:13 pm, Steve Litt wrote:
> > Hi all,
> >
> > I wrote some hierarchy handling classes in Node.rb
> > (http://www.troublesh.../projects/Node.rb...), 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?
> >
> > Thanks
> >
> > SteveT
>
> Whoops, the actual code is here:
> http://www.troublesh.../projects/Node.rb/downloads/0...., with
> test progarm here:
> http://www.troublesh.../projects/Node.rb/downloads/0.02/testnod...
> and test data here:
> http://www.troublesh.../projects/Node.rb/downloads/0.0...
>
> /testnode_parse < test.otl | less
>
> Thanks
>
> SteveT
>
> Steve Litt
> http://www.troublesh...
> slitt@troubleshooters.com

Hi Steve,

Here are some quick pointers:

* No need to subclass Object. All classes inherit from Object
automatically
* You don't need most of the explict getters and setters. That's what
attr_accessor is for
* Ditch the camel case
* No need for semicolons
* Rather than method names like "setParent", use "parent=".

Regards,

Dan

Ryan Leavengood

12/8/2005 2:50:00 AM

0

On 12/7/05, Steve Litt <slitt@earthlink.net> wrote:
>
> In future software, what could I do to write
> in a more Ruby-like fashion?

This definitely doesn't look like idiomatic Ruby, and here are a few
glaring things I can see:

1. The general naming convention is lowercase with underscores for
variables and methods. You have sort of a mix of "runitalltogether"
variables, i.e. prevsibling, and then the CamelCase methods like
insertSiblingAfterYou. Those should be prev_sibling and
insert_sibling_after_you (though that last one is a bit too long.)

2. Generally when initializing member variables from parameters in
initialize, parallel assignment is used, i.e. "@name, @type, @value =
name, type, value".

3. You have "class Node < Object", which is redundant since classes
subclass object by default:

irb(main):065:0> class Node;end
=> nil
irb(main):066:0> Node.superclass
=> Object

4. There is not a single block used in your code. Ruby without blocks
is like C++ without classes. In other words you can get by, but you
lose A LOT of power and beautiful code. Most of the those loops could
be iterators with blocks, especially this one:

for lineno in 1..lines.length
line = lines[lineno-1]
...

How about lines.each?

Ryan


Ara.T.Howard

12/8/2005 3:11:00 AM

0

Douglas Livingstone

12/8/2005 4:13:00 AM

0

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...), 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/...

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


Tim Hammerquist

12/8/2005 7:34:00 AM

0

Ryan Leavengood <leavengood@gmail.com> wrote:
> Steve Litt <slitt@earthlink.net> wrote:
> > In future software, what could I do to write
> > in a more Ruby-like fashion?
>
> This definitely doesn't look like idiomatic Ruby, and here are a few
> glaring things I can see:
>
> 1. The general naming convention is lowercase with underscores
> for variables and methods. You have sort of a mix of
> "runitalltogether" variables, i.e. prevsibling, and then the
> CamelCase methods like insertSiblingAfterYou. Those should be
> prev_sibling and insert_sibling_after_you (though that last
> one is a bit too long.)

This recurring label "CamelCase" (or sometimes "camelCase")
seems to come up a lot, and I can't help thinking "camel" is
a reference to Perl... This is strange for me, as a veteran
Perl programmer.

I've always read/heard/been told that the preferred method of
naming variables in Perl a_var_name style. People who used
StudlyCaps or javaCaps were usually accused of having come from
either Visual Basic (or other Win32 tech) or Java, respectively.

Upon looking, I do see some Perl modules using StudlyCaps
(DirHandle, FileCache, ExtUtils, et al), but this is hardly more
prominent than Ruby's MatchData, NilClass, FileTest, or
ObjectSpace.

I guess my comment is two-fold. One, can we really single out
Perl as the progenitor of this practice? And two, to state that
this naming scheme is not an endorsed practice for variable
names to my knowledge.

Cheers,
Tim Hammerquist

Tim Hammerquist

12/8/2005 7:47:00 AM

0

Tim Hammerquist <penryu@saiyix.ath.cx> wrote:
> I guess my comment is two-fold. One, can we really single out
> Perl as the progenitor of this practice? And two, to state
> that this naming scheme is not an endorsed practice for
> variable names to my knowledge.

Nor for method names.

> Cheers,
> Tim Hammerquist

Kevin Brown

12/8/2005 7:54:00 AM

0

On Thursday 08 December 2005 01:37, Tim Hammerquist wrote:
> Ryan Leavengood <leavengood@gmail.com> wrote:
> This recurring label "CamelCase" (or sometimes "camelCase")
> seems to come up a lot, and I can't help thinking "camel" is
> a reference to Perl...

Why? CamelCase simply refers to the humps that appear in the middle of words
when youUppercaseTheMiddleWord. At least that's how I've always thought of
it. I don't think anyone's pointing fingers at Perl. Hell, I came from C++
and my first real Ruby code has all methods named in lowerCamelCase. -_-