readers.each{|x| puts "Hi #{x},"}
As per request, I have created a thread for small programs that do
neat things (we are all true geeks at heart :) ). This all started
with my less than perfect Sierpinski Triangle generator:
ruby -e'32.times{|y|print" "*(31-y);(y+1).times{|x|print"
#{~y&x==0?"A":"."}"};puts}'
I originally build the sig for my /. account but I use it from time to
time on other things. This was greatly improved to:
ruby -le'32.times{|y|print" "*(31-y),(0..y).map{|x|~y&x>0?" .":" A"}}'
from input by the community. Now as for readability... ;) The code was
not intentionally obfuscated but ended up that ways as I compressed my
original script.
As I had said before in another thread (Ruby 2.0), I am experimenting
with some other one liners. One was my 1d cellular automata (see
http://en.wikipedia.org/wiki/Cellular_automaton for more info). I went
ahead and coded a small version that was originally 2d then changed to
1d because of size. This might explain the ugly code (my lack of sleep
would be the other ;) ). Here is a version (not my most compact but
the most interesting):
# So long it might as well be four of five lines not one...
# Don't try too hard to understand this one as it isn't worth it ;)
R={111=>rand.round,110=>rand.round,101=>rand.round,100=>rand.round,11=>rand.round,10=>rand.round,1=>rand.round,0=>rand.round};
i="";64.times{i<<rand.round.to_s};loop{puts
i.tr('10','#.');o=R[i[0,2].to_i].to_s;(i.size-2).times{|n|o<<R[i[n,3].to_i].to_s};o<<R[i[-2,2].to_i*10].to_s;i=o;sleep
0.1}
This one has a random rule set and a randomized initial generation.
Not very good at all. I posted to IRC to see what could get shortened
and got this from Christian Neukirchen (chris2 on freenode):
# no random and using rule 110
# This is probably better suited for a sig
t="."*60<<?#;loop{s,t,c=t,'',-1;s.each_byte{t<<(%'### #..
...'.include?((".#{s}..")[c+=1,3])?'.':'#')};puts t;sleep 0.1}
Quite impressive regrading readability :)
Do you guys have any neat one liners? Can you improve on this one?
cat /dev/random | ruby,
Brian Mitchell
---
ruby -e'puts [84,46,66,46,68,46].pack("c*")' # or
ruby -rbase64 -e'puts Base64.decode64("TG9uZyBsaXZlIFJ1Ynkh")'