1

enter code hereThis is a one liner to randomize STDIN, the aim is to feed a list of files and tail -N gives you N random out of a huge list (bigger than N anyway):

 ruby -e 'arr=[]; ARGF.each {|line| arr << line}; puts arr.sort_by {rand}'

I am wondering if there is a shorter version.

The winner is:

find . -type f | ruby -e 'puts ARGF.sort_by {rand}'

3 Answers 3

3

How about:

ruby -e 'puts ARGF.sort_by{rand}'
Sign up to request clarification or add additional context in comments.

3 Comments

It does not seem to work... "e:1:in each_line': private method rand' called for "./""
you mean: ruby -e 'puts ARGF.sort_by {rand}' and yes! :)
Sounds like a Ruby version conflict, the &:rand syntax works for me but your {rand} version is the same thing and even uses 2 fewer characters (if you get rid of that space :).
1

This what you're looking for?

ruby -e 'p ARGF.to_a.sample'

1 Comment

I need to randomize a list what I get on STDIN, see example above
0

Who needs stdin?

Dir["*"].select {|v| v if File.file?(v)}.sort_by {rand}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.