[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[SOT]Signatures and one liners

Brian Mitchell

12/6/2004 5:37:00 AM

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


22 Answers

Jamis Buck

12/6/2004 5:48:00 AM

0

On 14:37 Mon 06 Dec , Brian Mitchell wrote:
> Do you guys have any neat one liners? Can you improve on this one?

Mine was discussed previously a few times on this list:

ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'

(It should all be on one line.) It's a bit verbose. I remember others found
more compact ways of doing similar things. (Can't remember the thread, and I'm
too tired to do any serious looking right now anyway...)

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...



T. Onoma

12/6/2004 6:53:00 AM

0

On Monday 06 December 2004 12:37 am, Brian Mitchell wrote:
| Do you guys have any neat one liners? Can you improve on this one?

I like this one by Tadayoshi Funaba, mostly because it says "Begin landing
your troops" :)

[8,16,20,29,78,65,2,14,26,12,12,28,71,114,12,13,12,82,72,21,17,4,10,2,95].
each_with_index{|x,i| $><<(x^'Begin landing your troops'[i]).chr}


Tassilo Horn

12/6/2004 12:22:00 PM

0

Hi,

here's mine. It's not really tricky but it's fits into christmas time.

Regards,
Tassilo
--
def t(h) h.times{|l|(h-l+2*(l/3.to_i)).times{print " "}
((2*(l/3.to_i)+1)+2*(l%3)).times{print "*"};puts""};h.times{
print " "};puts '#' end;print "Height: ";t(gets.to_i)

Shajith

12/6/2004 12:46:00 PM

0

http://developers.slashdot.org/developers/04/12/06/1158251.shtml?tid=156...
"RAD in Ruby"

I didn't know KDevelop had Ruby support.

Cheers!
CT


martinus

12/6/2004 2:40:00 PM

0

I like this one:

d=13;x=Hash.new{|h,n|n<d ?[n]:h[n/d]+[n%d]};p x[3*0x58615c45*
0x8f5b008bc632a394816c3].map{|i|' ABEGLNORSTUY'[i]}.pack('C*')

martinus

martinus

12/6/2004 2:41:00 PM

0

I like this one:

d=13;x=Hash.new{|h,n|n<d ?[n]:h[n/d]+[n%d]};p x[3*0x58615c45*
0x8f5b008bc632a394816c3].map{|i|' ABEGLNORSTUY'[i]}.pack('C*')

martinus

Christian Neukirchen

12/6/2004 5:55:00 PM

0

On Mon, 6 Dec 2004 14:37:26 +0900, Brian Mitchell <binary42@gmail.com> wrote:
> 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 :)

Come on, this is really readable compared some some Perl around...
and did you already see my dc(1) hack? :)

> Do you guys have any neat one liners? Can you improve on this one?

How about this nice, 134 bytes CGI blog?:

puts"Content-type: text/html\n\n<h1>Blog",Dir["*.entry"].sort_by{|f|-File.
mtime(f).to_i}.first(9).map{|f|'<h2>'+File.read(f)+'<hr>'}

Happy hacking,
Christian Neukirchen


Christian Neukirchen

12/6/2004 6:16:00 PM

0

On Mon, 6 Dec 2004 18:54:29 +0100, Christian Neukirchen
<chneukirchen@gmail.com> wrote:
> How about this nice, 134 bytes CGI blog?:
>
> puts"Content-type: text/html\n\n<h1>Blog",Dir["*.entry"].sort_by{|f|-File.
> mtime(f).to_i}.first(9).map{|f|'<h2>'+File.read(f)+'<hr>'}

126 bytes now:

puts"Content-type: text/html\n\n<h1>Blog",Dir["*.entry"].sort_by{|f|-File.
mtime(f).to_i}[0,9].map{|f|"<h2>#{IO.read f}<hr>"}

> Happy hacking,
> Christian Neukirchen


gabriele renzi

12/6/2004 8:28:00 PM

0

Christian Neukirchen ha scritto:
> On Mon, 6 Dec 2004 18:54:29 +0100, Christian Neukirchen
> <chneukirchen@gmail.com> wrote:
>
>>How about this nice, 134 bytes CGI blog?:
>>
>>puts"Content-type: text/html\n\n<h1>Blog",Dir["*.entry"].sort_by{|f|-File.
>>mtime(f).to_i}.first(9).map{|f|'<h2>'+File.read(f)+'<hr>'}
>
>
> 126 bytes now:
>
> puts"Content-type: text/html\n\n<h1>Blog",Dir["*.entry"].sort_by{|f|-File.
> mtime(f).to_i}[0,9].map{|f|"<h2>#{IO.read f}<hr>"}
>

I had some fun playing the shortest wiki contest:
#!/path/per/ruby -r cgi
H='HomePage';B='w7.cgi?n=%s';p=(c=CGI.new('html4')).params;n=p['n'][0]||H
d=p['d'][0];t=IO.read(n)rescue '';open(n,'w').puts(t=CGI.escapeHTML(d))
if d c.out{c.h1{n}+c.a(B%H){H}+c.pre{ t.gsub(/([A-Z]\w+){2}/){|w|
c.a(B%w){w}}}+c.form("get"){c.textarea('d'){t}+c.hidden('n',n)+c.submit}}

the final version with hacks from Mauricio Fernandez was a 3 liner but I
can't find it :/

David Ross

12/7/2004 12:52:00 AM

0

CT wrote:

>http://developers.slashdot.org/developers/04/12/06/1158251.shtml?tid=156...
>"RAD in Ruby"
>
>I didn't know KDevelop had Ruby support.
>
>Cheers!
>CT
>
>
>
>
KDevelop uses Kate by default. Its even better in this recent
release because the regex engine. The past releases engine
was not too great, so it was difficult to implement a few
specifications. The syntax highlighting uses regexes to identify
keywords. :)

David Ross