开发者

Request for comments: Ruby script that counts the length of a MySQL table name

开发者 https://www.devze.com 2022-12-28 10:07 出处:网络
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:

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 c开发者_如何学Pythonomments. Thanks in advance


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"


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})" .

0

精彩评论

暂无评论...
验证码 换一张
取 消