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.
rtilley (Guest)
on 2006-03-06 22: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
Edward F. (Guest)
on 2006-03-06 22: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
James G. (Guest)
on 2006-03-06 22: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 G. II
(Guest)
on 2006-03-06 23:04
(Received via mailing list)
rtilley (Guest)
on 2006-03-06 23:26
(Received via mailing list)
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.
rtilley (Guest)
on 2006-03-06 23: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
Logan C. (Guest)
on 2006-03-06 23: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}"
Han H. (Guest)
on 2006-03-06 23:47
(Received via mailing list)
On 3/6/06, rtilley <removed_email_address@domain.invalid> 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.
Robert K. (Guest)
on 2006-03-06 23:53
(Received via mailing list)
2006/3/6, rtilley <removed_email_address@domain.invalid>:
> 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
Logan C. (Guest)
on 2006-03-06 23:53
(Received via mailing list)
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 <g>:

puts %w[ name processid executablepath ].map { |x| p.send(x) }.join
("\t")
William J. (Guest)
on 2006-03-07 05:39
(Received via mailing list)
Logan C. 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.