tags:

views:

39

answers:

2

Hi, I'm new at ruby and I would like to ask you guys if there's something that could improve my Ruby code. Here's my script:

#!/usr/bin/ruby -w

require 'mysql'
dbh = Mysql.real_connect('localhost', 'db_user', 'password', 'db_table')
tables = dbh.query('show tables')

tables.each do |table|
    puts "#{table}" + " (" + "#{table}".length.to_s + ")"
end

I'd love to hear your comments. Thanks in advance

+1  A: 

Small detail, but either of these looks cleaner, IMHO -- especially the first one, because it allows you to visualize the output layout in a quick glance:

printf "%s (%i)\n", table, table.to_s.length

print table, " (", table.to_s.length, ")\n"
FM
+1  A: 

Looks fine, minor change that I would do is when you are printing the string. Instead of concatenating multiple string, just place everything in a single string.

Therefore change this:

puts "#{table}" + " (" + "#{table}".length.to_s + ")" to

puts "#{table} (#{table.length})" .

Pran