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
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
On Mar 6, 2006, at 2:43 PM, rtilley wrote:
Is there a right way and a wrong way to code in Ruby?
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 G. II
Edward F. 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.
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
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}”
On 3/6/06, rtilley [email protected] 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 H.
2006/3/6, rtilley [email protected]:
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
On Mar 6, 2006, at 4:43 PM, Logan C. wrote:
puts “#{p.name}\t#{p.processid}\t#{p.executablepath}”
Actually to be idiotically idiomatic :
puts %w[ name processid executablepath ].map { |x| p.send(x) }.join
("\t")