idiomatic ruby

Discussion in 'Ruby' started by rtilley, Mar 6, 2006.

  1. rtilley

    rtilley Guest

    Is there a right way and a wrong way to code in Ruby?

    I just started coding some in Ruby last Friday. Since then, I've written
    several small scripts on Windows using WMI with Ruby. My scripts work
    fine, I just worry that I'll develop bad habits or that I'm not
    programming idiomatic Ruby. Is there such as thing as idiomatic Ruby?
    Python hackers call idiomatic Python 'Pythonic' and code that is not
    Pythonic is frowned upon. They are rather rigid, but I like Python OK.

    My goal now is to port much of my Python code to Ruby as a learning
    experience. So, should I post code here for comments and suggestions?
    Or, is it safe to assume if my Ruby scripts work as I expect them to
    that they are OK?

    Many thanks,

    Brad
     
    rtilley, Mar 6, 2006
    #1
    1. Advertising

  2. --PEIAKu/WMn1b1Hv9
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On Tue, Mar 07, 2006 at 05:43:40AM +0900, rtilley wrote:
    > Is there such as thing as idiomatic Ruby?=20


    Yes.

    > Python hackers call idiomatic Python 'Pythonic' and code that is not
    > Pythonic is frowned upon. They are rather rigid, but I like Python
    > OK.


    I think you'll find the Ruby community more flexible. There's more
    than one way to do it here.

    > So, should I post code here for comments and suggestions?=20


    Absolutely, that's what ruby-talk is for.

    regards,
    Ed

    --PEIAKu/WMn1b1Hv9
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFEDKDjnhUz11p9MSARAv0EAKDPOawGJ3ZzDh03nKJDcHTdQrFDlgCgsbjQ
    MbSSmQSb/DRQMKlCz8e+RxM=
    =gWYa
    -----END PGP SIGNATURE-----

    --PEIAKu/WMn1b1Hv9--
     
    Edward Faulkner, Mar 6, 2006
    #2
    1. Advertising

  3. On Mar 6, 2006, at 2:43 PM, rtilley wrote:

    > Is there a right way and a wrong way to code in Ruby?


    <laughs> Yes. Go to your room! ;)

    Naturally, there are non-ideal ways to use any language. Ruby is no
    different.

    That said, if your code is working that's the most important thing.
    We won't come and taking your keyboard away for ugly code.

    > My goal now is to port much of my Python code to Ruby as a learning
    > experience. So, should I post code here for comments and suggestions?


    I think posting an early project or two is a great idea. We can
    probably give a tip or two that will prevent mistakes down the line.

    Welcome to Ruby!

    James Edward Gray II
     
    James Edward Gray II, Mar 6, 2006
    #3
  4. rtilley

    Guest

    , Mar 6, 2006
    #4
  5. rtilley

    rtilley Guest

    Edward Faulkner wrote:
    >>So, should I post code here for comments and suggestions?

    >
    > Absolutely, that's what ruby-talk is for.


    Thanks guys... I'll post something when I have enough for y'all to look
    over.
     
    rtilley, Mar 6, 2006
    #5
  6. rtilley

    rtilley Guest

    This is probably enough for the list to look over. What do you guys
    think -- idiomatic Ruby or not? Also, if I'm doing anything that's not
    safe (should I close 'mgmt' or 'net' somehow) please let me know.

    Thanks again for the feedback.

    require 'win32ole'

    # Get running process names, ids and paths (if present)
    mgmt = WIN32OLE.connect("winmgmts:\\\\.")
    mgmt.InstancesOf("win32_process").each do |p|
    puts p.name.to_s + "\t" +
    p.processid.to_s + "\t" +
    p.executablepath.to_s
    end

    puts

    # Get the IP, Mac Address and Gateway of Active Network Interfaces.
    net = WIN32OLE.connect("winmgmts:\\\\.")
    net.InstancesOf("win32_networkadapterconfiguration").each do |i|
    if i.ipenabled == false
    #puts "Interface Disabled"
    else
    puts i.ipaddress
    puts i.defaultipgateway
    puts i.macaddress
    end
    end
     
    rtilley, Mar 6, 2006
    #6
  7. --Apple-Mail-23-165457559
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    format=flowed


    On Mar 6, 2006, at 4:38 PM, rtilley wrote:

    > mgmt.InstancesOf("win32_process").each do |p|
    > puts p.name.to_s + "\t" +
    > p.processid.to_s + "\t" +
    > p.executablepath.to_s
    > end


    I would use string interporlation here:
    puts "#{p.name}\t#{p.processid}\t#{p.executablepath}"


    --Apple-Mail-23-165457559--
     
    Logan Capaldo, Mar 6, 2006
    #7
  8. rtilley

    Han Holl Guest

    ------=_Part_197_4708100.1141681641679
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 3/6/06, rtilley <> wrote:
    >
    > This is probably enough for the list to look over. What do you guys
    > think -- idiomatic Ruby or not? Also, if I'm doing anything that's not
    > safe (should I close 'mgmt' or 'net' somehow) please let me know.
    >
    > Thanks again for the feedback.
    >
    > require 'win32ole'
    >
    > # Get running process names, ids and paths (if present)
    > mgmt =3D WIN32OLE.connect("winmgmts:\\\\.")
    > mgmt.InstancesOf("win32_process").each do |p|
    > puts p.name.to_s + "\t" +
    > p.processid.to_s + "\t" +
    > p.executablepath.to_s
    > end



    I would do:
    mgmt.InstancesOf("win32_process").each do |p|
    puts "#{p.name}\t#{p.processid}\t#{p.executablepath}"
    end

    puts
    >
    > # Get the IP, Mac Address and Gateway of Active Network Interfaces.
    > net =3D WIN32OLE.connect("winmgmts:\\\\.")
    > net.InstancesOf("win32_networkadapterconfiguration").each do |i|
    > if i.ipenabled =3D=3D false
    > #puts "Interface Disabled"
    > else
    > puts i.ipaddress
    > puts i.defaultipgateway
    > puts i.macaddress
    > end
    > end



    I would use i only for (small) integers, but it's a question of taste of
    course.

    Cheers,

    Han Holl

    ------=_Part_197_4708100.1141681641679--
     
    Han Holl, Mar 6, 2006
    #8
  9. 2006/3/6, rtilley <>:
    > This is probably enough for the list to look over. What do you guys
    > think -- idiomatic Ruby or not? Also, if I'm doing anything that's not
    > safe (should I close 'mgmt' or 'net' somehow) please let me know.
    >
    > Thanks again for the feedback.
    >
    > require 'win32ole'
    >
    > # Get running process names, ids and paths (if present)
    > mgmt =3D WIN32OLE.connect("winmgmts:\\\\.")
    > mgmt.InstancesOf("win32_process").each do |p|
    > puts p.name.to_s + "\t" +
    > p.processid.to_s + "\t" +
    > p.executablepath.to_s
    > end


    Note that you can use print with several arguments - that way you
    don't need to create the complete string in mem and then write it (not
    a performance problem in this small piece of code but I may be in
    other circumstances). You can do

    print p.name, "\t",
    p.processid, "\t",
    p.executablepath, "\n"

    There are tons of other ways including printf etc.

    > puts
    >
    > # Get the IP, Mac Address and Gateway of Active Network Interfaces.
    > net =3D WIN32OLE.connect("winmgmts:\\\\.")
    > net.InstancesOf("win32_networkadapterconfiguration").each do |i|
    > if i.ipenabled =3D=3D false
    > #puts "Interface Disabled"
    > else
    > puts i.ipaddress
    > puts i.defaultipgateway
    > puts i.macaddress
    > end
    > end


    This looks pretty neat. Some minor remarks: don't compare booleans -
    just use them. In your case

    if i.ipenabled
    puts i.ipaddress, i.defaultgateway, i.macaddress
    else
    puts "disabled"
    end

    Or the other way round if you prefer:

    unless i.ipenabled
    puts "disabled"
    else
    ...
    end

    Kind regards

    robert


    --
    Have a look: http://www.flickr.com/photos/fussel-foto/
     
    Robert Klemme, Mar 6, 2006
    #9
  10. --Apple-Mail-24-165942694
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed


    On Mar 6, 2006, at 4:43 PM, Logan Capaldo wrote:

    >
    > On Mar 6, 2006, at 4:38 PM, rtilley wrote:
    >
    >> mgmt.InstancesOf("win32_process").each do |p|
    >> puts p.name.to_s + "\t" +
    >> p.processid.to_s + "\t" +
    >> p.executablepath.to_s
    >> end

    >
    > I would use string interporlation here:
    > puts "#{p.name}\t#{p.processid}\t#{p.executablepath}"
    >


    Actually to be idiotically idiomatic <g>:

    puts %w[ name processid executablepath ].map { |x| p.send(x) }.join
    ("\t")


    --Apple-Mail-24-165942694--
     
    Logan Capaldo, Mar 6, 2006
    #10
  11. Logan Capaldo wrote:
    > On Mar 6, 2006, at 4:43 PM, Logan Capaldo wrote:
    >
    > >
    > > On Mar 6, 2006, at 4:38 PM, rtilley wrote:
    > >
    > >> mgmt.InstancesOf("win32_process").each do |p|
    > >> puts p.name.to_s + "\t" +
    > >> p.processid.to_s + "\t" +
    > >> p.executablepath.to_s
    > >> end

    > >
    > > I would use string interporlation here:
    > > puts "#{p.name}\t#{p.processid}\t#{p.executablepath}"
    > >

    >
    > Actually to be idiotically idiomatic <g>:
    >
    > puts %w[ name processid executablepath ].map { |x| p.send(x) }.join
    > ("\t")
    >
    >
    > --Apple-Mail-24-165942694--


    puts %w}name processid executablepath}.inject(""){|s,x|
    s+p.send(x)+"\t" }.chop
     
    William James, Mar 7, 2006
    #11
    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. Iwan van der Kleyn
    Replies:
    5
    Views:
    161
    James Edward Gray II
    Nov 23, 2004
  2. Zed A. Shaw
    Replies:
    2
    Views:
    109
    Zed A. Shaw
    Apr 23, 2005
  3. Brock Weaver

    Idiomatic ruby version of this code?

    Brock Weaver, Aug 18, 2005, in forum: Ruby
    Replies:
    16
    Views:
    176
    pat eyler
    Aug 23, 2005
  4. Idiomatic ruby

    , Feb 12, 2006, in forum: Ruby
    Replies:
    5
    Views:
    121
    Robert Klemme
    Feb 13, 2006
  5. Idiomatic ruby

    , Feb 13, 2006, in forum: Ruby
    Replies:
    4
    Views:
    158
    Adam P. Jenkins
    Feb 13, 2006
Loading...

Share This Page