mysqlguy.net

Ruby Succinctness competition

Submitted by jay on February 14, 2008 - 9:18am

For some reason I never was that excited about trying to accomplish these sorts of things in Perl:

1..rand(256)).inject("") {|string,n| string + ('a'..'z').to_a[rand(25)] }

This generates a string from 1 to 256 characters long, containing random letters from 'a' to 'z'.  

Normally, I wouldn't use something that's quite so unreadable, but I'm starting to like Ruby's way of taking what should be a very simple operation that I might have done in 3-5 lines in Perl and boiling it down to a single line.

I'd love to see if anyone can make this more succinct (but one line is still a requirement!)

Here's the code in it's context. It's just a test script writing into my partition table every minute so I can verify my Events from previous posts are working correctly.

#!/usr/bin/ruby

require 'mysql'


dbh = Mysql.new( "server.domain.com",
    "user", "pass", "test", 3306)

loop do
    string = ""; rand(256).times { string << ('a'..'z').to_a[rand(25)] }
    dbh.query( "INSERT INTO log (logged, text) VALUES (
'#{Time.now.strftime( '%Y-%m-%d %H:%M:%S')}', 
'#{(1..rand(256)).inject("") {|string,n| string + ('a'..'z').to_a[rand(25)] }}'
)" );
    sleep 60
end

Trackback URL for this post:

http://mysqlguy.net/trackback/10

in python:import

in python:

import random,string
string="".join([random.choice(string.lowercase) for i in range(0,int(random.uniform(1,256)))])

does importing libraries count towards a one liner?  i guess they should... but python doesnt have a rand function in the global ns.

jay's picture

A shorter line: +1 For using

A shorter line: +1
For using Python and not Ruby: -2 ;)

Not realizing it wasn't Ruby until I started typing it in: -10 (for me)

I'd suggest this in perl:

I'd suggest this in perl:

$rnd = join "", map { (a..z)[rand 26] } 1..rand 256;

Neither unreadable nor long-winded :-)

I was going to submit

I was going to submit something very similar to rhesa.  It's not so much a language thing, it's a way of thinking.  Ruby encourages that.  Perl allows it.  Java doesn't :-)  Regardless of the language, functional programming rocks for things like this.

Ruby:(1..rand(256)).map{(97+r

Ruby:
(1..rand(256)).map{(97+rand(26)).chr}.join

jay's picture

Exactly what I was looking

Exactly what I was looking for, thanks!  I'm still at the point with my Ruby that I need to keep rereading the function lists in the core/standard library to find functions like map and make sure I know what they do....

Post new comment

The content of this field is kept private and will not be shown publicly.

Warning!

Comment abuse is not tolerated on this site, besides all the comments are moderated, so don't bother posting comments that are not on topic, only for increasing the SEO of your site, or are outright spam.  If you've got something intelligent to contribute, by all means, post a link to your blog.  

MySQL

Yahoo

Recent comments

About Me

Jay Janssen
Yahoo!, Inc.
jayj at yahoo dash inc dot com

MySQL
High Availability
Global Load Balancing
Failover

View Jay Janssen's LinkedIn profileView Jay Janssen's Facebook profile

User login

Friends

Links