They say I write Ruby like Perl

S

Steve Litt

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?

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.troubleshooters.com
 
S

Steve Litt

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?

Thanks

SteveT

Whoops, the actual code is here:
http://www.troubleshooters.com/projects/Node.rb/downloads/0.02/Node.rb, with
test progarm here:
http://www.troubleshooters.com/projects/Node.rb/downloads/0.02/testnode_parse.rb
and test data here:
http://www.troubleshooters.com/projects/Node.rb/downloads/0.02/test.otl

/testnode_parse < test.otl | less

Thanks

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
L

Logan Capaldo

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?

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.troubleshooters.com

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, ;)
 
D

dblack

Hi --

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?

For style things (avoiding camelCase for methods and local variables,
two-character indentation, etc.), you can look at (most of :) the
Ruby code in the standard library. Also there's a good style guide
at:
http://pub.cozmixng.org/~the-rwiki/rw-cgi.rb?cmd=view;name=RubyCodingConvention
(not beyond dispute in every detail but very much in harmony with
traditional Ruby style).

Looking a bit at the code itself, I think there's a certain amount of
verbosity that you could cut down on -- and still be nice and clear
(which is an area where Ruby shines). For example, you have this:

def counttabs(s)
answer = s.index(/[^\t]/)
answer = 0 if not answer
return answer
end

which could be:

def counttabs(s)
s.index(/[^\t]/) || 0
end

(I'm not sure about the name -- it's not actually counting tabs -- but
I'll leave that to you :) Of course there's no imperative to make
things as short as they can be -- but one thing I like about Ruby is
that one's code tends to get more concise *and* to get more clear, as
one works on a project.


David
 
D

Daniel Berger

Steve said:

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
 
R

Ryan Leavengood

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 =3D
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
=3D> nil
irb(main):066:0> Node.superclass
=3D> 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
=09=09=09line =3D lines[lineno-1]
...

How about lines.each?

Ryan
 
A

ara.t.howard

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?

here are some thoughts:

class Node < Object
def initialize(nam, type, value)
super()
@name = nam
@type = type
@value = value
@attribs = {}
@parent = nil
@prevsibling = nil
@nextsibling = nil
@firstchild = nil
@lastchild = nil
end

attr_reader :name, :type, :value
attr_writer :name, :type, :value
attr_reader :parent, :prevsibling, :nextsibling, :firstchild, :lastchild

def setAttributes(attribs) @attribs = attribs; end
def getAttributes() return @attribs; end
def hasAttributes() return @attribs != nil; end
def setAttribute(key, value) @attribs[key] = value; end
def getAttribute(key) return @attribs[key]; end
def hasAttribute(key) return @attribs[key] != nil; end

i would condensed into

class Node

ATTRIBUTES = %w(
name type value attribs parent prevsibling nextsibling firstchild lastchild
).each{|a| attr_accessor a; alias_method "#{ a }?", a}

def initialize name, type, value, attribs = {}
@name, @type, @value, @attribs = name, type, value, attribs
end

don't bother with access control - sometimes it's needed (object available on
the web) but sometimes it's not. with it you are saying "my code has no
mistakes - you will not need access to these vars" by saying 'read_only'. i've
probably fixed about 50 bugs in code where i had to subvert the access control
by doing

obj.instance_var_set "@broken", "fixed"

because of this kind of design. don't use it where it's not critical.

dont' use get/set/has - use attr, attr=, attr?

def setParent(parent)
@parent = parent
node = self
while node.prevsibling
node = node.prevsibling
end
@parent.firstchild = node
node = self
while node.nextsibling
node = node.nextsibling
end
@parent.lastchild = node
@parent
end

could be

def parent= parent
parent.firstchild = oldest
parent.lastchild = youngest
@parent = parent
end
def oldest
node = self
node = node.prevsibling while node.prevsibling
node
end
def youngest
node = self
node = node.nextsibling while node.nextsibling
node
end

which isn't shorter - until the next time you write that code.

food for thought.

cheers.

-a
--
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================
 
D

Douglas Livingstone

2005/12/8 said:
Hi all,

I wrote some hierarchy handling classes in Node.rb
(http://www.troubleshooters.com/projects/Node.rb/index.htm), and I've bee= n
told I write Ruby in Perl style. In future software, what could I do to w= rite
in a more Ruby-like fashion?

Hi Steve,

From the testing code, you have:

def rvExit(checker, level)
=09=09for i in 0...level
=09=09=09print "\t"
=09=09end
=09=09print checker.value
=09=09print ", 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 []=3D 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)
=09=09regex_comment =3D Regexp.new("^\\s*" + @commentchar )
=09=09if @skipblanks and line =3D~ /^\s*$/
=09=09=09return true
=09=09elsif @commentchar and regex_comment.match(line)
=09=09=09return true
=09=09else
=09=09=09return false
=09=09end
end

could be:

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

You get the idea :)

Good thread, thanks!

hth,
Douglas
 
K

Kevin Brown

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. -_-
 
D

Dan Diebolt

--0-181455393-1134032655=:15176
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Hell, I came from C++ and my first real Ruby code has all methods named=
in lowerCamelCase.=20

"There is no place in Ruby where lowerCamelCase is ever used. "
http://www.rubygarden.org/ruby?RubyStyleGuide
=20
See Also slide 10 of Schroeder's Ruby Course:

I Variables and methods should be written in snake_case
I Class Names should be written in CamelCase
I Constants should be written ALL_UPPERCASE
=20
http://ruby.brian-schroeder.de/course/slides.pdf

=09
 
S

Steve Litt

On Thursday 08 December 2005 02:37 am, Tim Hammerquist wrote:
[clip]
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...

Hi Tim,

I had assumed it was called "CamelCase" because it had humps :)

SteveT

Steve Litt
http://www.troubleshooters.com
(e-mail address removed)
 
T

tony summerfelt

Steve Litt wrote on 12/8/2005 7:17 AM:
I had assumed it was called "CamelCase" because it had humps :)

the o'reilly "programming perl" book features a camel on the front.
just a guess :)
 
D

dblack

Hi --

Who says? Just a style/convention issue surely. Readability doesn't
suffer, nor does execution.

"Just"? :) You're right, though, in the sense that the parser
doesn't enforce traditional style. You can write Ruby that looks like
lots of other languages. Then when you go back to those languages,
you can write variable names like_this because you now come from
Ruby....

Or just break the cycle and use the conventions of whatever language
you're using *when* you're using it :)


David
 
J

James Edward Gray II

Who says? Just a style/convention issue surely. Readability doesn't
suffer, nor does execution.

It's not a syntax rule of Ruby, no. But part of learning a language
is learning to speak it as the native speakers do.

James Edward Gray II
 
R

Rich

I'd be interested to know *why* it is a language convention, and more
important why it is a lanaguage convention that from this thread it
seems rubyists vigorously insist upon? For those of us who were
"raised" with a language like Java (or as some on this list may say,
brain damaged ;-), lowerCamelCase seems more pleasing to the eye than
identifiers_with_underscores.
 
B

Bill Guindon

Who says? Just a style/convention issue surely. Readability doesn't
suffer, nor does execution.

Personally, I think readability does suffer. The wordier it gets, the
more I have to think. Why not a simple 'append_sibling'?
 
C

Chad Perrin

I'd be interested to know *why* it is a language convention, and more
important why it is a lanaguage convention that from this thread it
seems rubyists vigorously insist upon? For those of us who were
"raised" with a language like Java (or as some on this list may say,
brain damaged ;-), lowerCamelCase seems more pleasing to the eye than
identifiers_with_underscores.

More to the point, from my point of view, it's easier to type without
being made unreadable (where it == lowerCamelCase) and actually uses
rather less keystrokes.

That being said, however, I tend to be willing to bend to convention on
matters like this: it makes collaboration easier.
 
F

Francois Paul

conventions make it easier for collaboration. If you are not interested
in sharing code, or can't forsee anybody else having to maintain your
code - then obviously do as you wish.
but if you were to submit to collaborative projects or you want to learn
from reading other people's code it would be wize to follw the conventions.
The conventions help readability in the sense that different 'parts of
speach' is obvious to the eye.
 
J

James Edward Gray II

I'd be interested to know *why* it is a language convention, and more
important why it is a lanaguage convention that from this thread it
seems rubyists vigorously insist upon? For those of us who were
"raised" with a language like Java (or as some on this list may say,
brain damaged ;-), lowerCamelCase seems more pleasing to the eye than
identifiers_with_underscores.

We feel exactly the opposite. That's your *why*. ;)

The human eye picks out words by shape. We use spaces between words
to separate those shapes into easily digested chunks. OneLongWord is
not a shape we are use to, so we have to stop and think.
Unfortunately, spaces aren't allowed in programming variables. The _
character is allowed though and the next best thing, so we go with that.

This is all my opinion, of course.

James Edward Gray II
 
J

Joe Van Dyk

More to the point, from my point of view, it's easier to type without
being made unreadable (where it =3D=3D lowerCamelCase) and actually uses
rather less keystrokes.

Don't see how using lowerCamelCase uses less keystrokes than
lower_camel_case. I pressed 16 keys to type of of them. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top