idiomatic ruby

R

rtilley

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
 
E

Edward Faulkner

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

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

James Edward Gray II

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
 
R

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

Logan Capaldo

--Apple-Mail-23-165457559
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed


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

Han Holl

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

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

Robert Klemme

2006/3/6 said:
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
 
L

Logan Capaldo

--Apple-Mail-24-165942694
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed


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

William James

Logan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top