Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > They say I write Ruby like Perl

Reply
Thread Tools

They say I write Ruby like Perl

 
 
Steve Litt
Guest
Posts: n/a
 
      12-08-2005
Hi all,

I wrote some hierarchy handling classes in Node.rb
(http://www.troubleshooters.com/proje...e.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


 
Reply With Quote
 
 
 
 
Steve Litt
Guest
Posts: n/a
 
      12-08-2005
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/proje...e.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/proje...s/0.02/Node.rb, with
test progarm here:
http://www.troubleshooters.com/proje...tnode_parse.rb
and test data here:
http://www.troubleshooters.com/proje.../0.02/test.otl

/testnode_parse < test.otl | less

Thanks

SteveT

Steve Litt
http://www.troubleshooters.com
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      12-08-2005

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

> Hi all,
>
> I wrote some hierarchy handling classes in Node.rb
> (http://www.troubleshooters.com/proje...e.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,



 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      12-08-2005
Hi --

On Thu, 8 Dec 2005, Steve Litt wrote:

> Hi all,
>
> I wrote some hierarchy handling classes in Node.rb
> (http://www.troubleshooters.com/proje...e.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/r...dingConvention
(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

--
David A. Black
(E-Mail Removed)

"Ruby for Rails", forthcoming from Manning Publications, April 2006!


 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      12-08-2005
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.troubleshooters.com/proje...e.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/proje...s/0.02/Node.rb, with
> test progarm here:
> http://www.troubleshooters.com/proje...tnode_parse.rb
> and test data here:
> http://www.troubleshooters.com/proje.../0.02/test.otl
>
> /testnode_parse < test.otl | less
>
> Thanks
>
> SteveT
>
> Steve Litt
> http://www.troubleshooters.com
> (E-Mail Removed)


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

 
Reply With Quote
 
Ryan Leavengood
Guest
Posts: n/a
 
      12-08-2005
On 12/7/05, Steve Litt <(E-Mail Removed)> 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 =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


 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      12-08-2005
On Thu, 8 Dec 2005, Steve Litt wrote:

> Hi all,
>
> I wrote some hierarchy handling classes in Node.rb
> (http://www.troubleshooters.com/proje...e.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 arent, revsibling, :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
================================================== =============================



 
Reply With Quote
 
Douglas Livingstone
Guest
Posts: n/a
 
      12-08-2005
2005/12/8, Steve Litt <(E-Mail Removed)>:
> Hi all,
>
> I wrote some hierarchy handling classes in Node.rb
> (http://www.troubleshooters.com/proje...e.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/libdo...doc/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


 
Reply With Quote
 
Kevin Brown
Guest
Posts: n/a
 
      12-08-2005
On Thursday 08 December 2005 01:37, Tim Hammerquist wrote:
> Ryan Leavengood <(E-Mail Removed)> 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. -_-


 
Reply With Quote
 
Dan Diebolt
Guest
Posts: n/a
 
      12-08-2005
--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
---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping=20
--0-181455393-1134032655=:15176--


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass the peas like they used to say =?Utf-8?B?SVQtTU9ORVk=?= MCSE 2 03-12-2007 05:16 PM
You say SIM, I say SEM Anon Computer Security 1 03-18-2006 01:49 PM
OT: and they say hell hath no fury like a WOMAN scorned Neil MCSE 0 05-27-2005 07:51 PM
RubyConf Hotel -- they now say they *do* have rooms David A. Black Ruby 2 08-19-2004 08:16 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments