They say I write Ruby like Perl

Discussion in 'Ruby' started by Steve Litt, Dec 8, 2005.

  1. Steve Litt

    Steve Litt Guest

    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
    Steve Litt, Dec 8, 2005
    #1
    1. Advertising

  2. Steve Litt

    Steve Litt Guest

    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?
    >
    > 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
    Steve Litt, Dec 8, 2005
    #2
    1. Advertising

  3. 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/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, ;)
    Logan Capaldo, Dec 8, 2005
    #3
  4. Steve Litt

    Guest

    Hi --

    On Thu, 8 Dec 2005, 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?
    >


    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

    --
    David A. Black


    "Ruby for Rails", forthcoming from Manning Publications, April 2006!
    , Dec 8, 2005
    #4
  5. 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/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
    >


    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
    Daniel Berger, Dec 8, 2005
    #5
  6. On 12/7/05, Steve Litt <> 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
    Ryan Leavengood, Dec 8, 2005
    #6
  7. Steve Litt

    Guest

    On Thu, 8 Dec 2005, 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?


    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
    ===============================================================================
    , Dec 8, 2005
    #7
  8. 2005/12/8, 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 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
    Douglas Livingstone, Dec 8, 2005
    #8
  9. Steve Litt

    Kevin Brown Guest

    On Thursday 08 December 2005 01:37, Tim Hammerquist wrote:
    > Ryan Leavengood <> 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. -_-
    Kevin Brown, Dec 8, 2005
    #9
  10. Steve Litt

    Dan Diebolt Guest

    --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--
    Dan Diebolt, Dec 8, 2005
    #10
  11. Steve Litt

    Steve Litt Guest

    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
    Steve Litt, Dec 8, 2005
    #11
  12. Steve Litt wrote on 12/8/2005 7:17 AM:

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


    > 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 :)

    --
    http://home.cogeco.ca/~tsummerfelt1
    telnet://ventedspleen.dyndns.org
    tony summerfelt, Dec 8, 2005
    #12
  13. Steve Litt

    Guest

    Hi --

    On Thu, 8 Dec 2005, Chris Game wrote:

    > Ryan Leavengood wrote:
    >
    >> 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.)

    >
    > 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

    --
    David A. Black


    "Ruby for Rails", forthcoming from Manning Publications, April 2006!
    , Dec 8, 2005
    #13
  14. On Dec 8, 2005, at 7:27 AM, Chris Game wrote:

    > 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
    James Edward Gray II, Dec 8, 2005
    #14
  15. Steve Litt

    Rich Guest

    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.

    On 12/8/05, James Edward Gray II <> wrote:
    > On Dec 8, 2005, at 7:27 AM, Chris Game wrote:
    >
    > > 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
    >
    >
    Rich, Dec 8, 2005
    #15
  16. Steve Litt

    Bill Guindon Guest

    On 12/8/05, Chris Game <> wrote:
    > Ryan Leavengood wrote:
    >
    > > 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.)

    >
    > 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'?

    > --
    > Chris Game
    >
    > Computers are only human.
    >
    >



    --
    Bill Guindon (aka aGorilla)
    Bill Guindon, Dec 8, 2005
    #16
  17. Steve Litt

    Chad Perrin Guest

    On Thu, Dec 08, 2005 at 11:27:23PM +0900, Rich wrote:
    > 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.

    --
    Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

    unix virus: If you're using a unixlike OS, please forward
    this to 20 others and erase your system partition.
    Chad Perrin, Dec 8, 2005
    #17
  18. 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.

    Rich wrote:

    >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.
    >
    >On 12/8/05, James Edward Gray II <> wrote:
    >
    >
    >>On Dec 8, 2005, at 7:27 AM, Chris Game wrote:
    >>
    >>
    >>
    >>>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
    >>
    >>
    >>
    >>

    >
    >
    >
    >
    Francois Paul, Dec 8, 2005
    #18
  19. On Dec 8, 2005, at 8:27 AM, Rich wrote:

    > 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
    James Edward Gray II, Dec 8, 2005
    #19
  20. Steve Litt

    Joe Van Dyk Guest

    On 12/8/05, Chad Perrin <> wrote:
    > On Thu, Dec 08, 2005 at 11:27:23PM +0900, Rich wrote:
    > > 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 =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. :)
    Joe Van Dyk, Dec 8, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Hester
    Replies:
    3
    Views:
    3,478
    Carl Prothman [MVP]
    Aug 11, 2003
  2. U S Contractors Offering Service A Non-profit

    " Hind sight is that of 20/20 they say ? "

    U S Contractors Offering Service A Non-profit, Nov 7, 2006, in forum: C Programming
    Replies:
    0
    Views:
    305
    U S Contractors Offering Service A Non-profit
    Nov 7, 2006
  3. U S Contractors Offering Service A Non-profit

    " They say why buy the cOw when the milk is fOr frEE "

    U S Contractors Offering Service A Non-profit, Nov 30, 2006, in forum: C Programming
    Replies:
    0
    Views:
    613
    U S Contractors Offering Service A Non-profit
    Nov 30, 2006
  4. HG

    Schemas they say ...

    HG, Nov 12, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    131
    Dan Rogers
    Nov 18, 2004
  5. David A. Black
    Replies:
    2
    Views:
    213
    Tim Hunter
    Aug 19, 2004
Loading...

Share This Page