Forum: Ruby idiomatic ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-06 21:46
(Received via mailing list)
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
036a1b88dafaab8ffd73a8b0a74b5b38?d=identicon&s=25 Edward Faulkner (Guest)
on 2006-03-06 21:55
(Received via mailing list)
On Tue, Mar 07, 2006 at 05:43:40AM +0900, rtilley wrote:
> Is there such as thing as idiomatic Ruby?

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?

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

regards,
Ed
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-06 21:58
(Received via mailing list)
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
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 rmagick@gmail.com (Guest)
on 2006-03-06 22:04
(Received via mailing list)
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-06 22:26
(Received via mailing list)
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.
02bd6b98b7c04f9ae5868eda3d01fb73?d=identicon&s=25 rtilley (Guest)
on 2006-03-06 22:41
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-06 22:44
(Received via mailing list)
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}"
2fce3b5a094cd28a0538bc4b314d1c09?d=identicon&s=25 Han Holl (Guest)
on 2006-03-06 22:47
(Received via mailing list)
On 3/6/06, rtilley <rtilley@vt.edu> wrote:
> 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


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

puts
>    end
> end


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

Cheers,

Han Holl
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-06 22:53
(Received via mailing list)
2006/3/6, rtilley <rtilley@vt.edu>:
> 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 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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-06 22:53
(Received via mailing list)
On Mar 6, 2006, at 4:43 PM, Logan Capaldo wrote:

> 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")
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-07 04:39
(Received via mailing list)
Logan Capaldo wrote:
> >
> --Apple-Mail-24-165942694--
puts %w}name processid executablepath}.inject(""){|s,x|
s+p.send(x)+"\t" }.chop
This topic is locked and can not be replied to.