Ruby 2.0

S

Shashank Date

Brian said:

And we (my son and I) are struggling with writing the recursive version
of it in MSWLogo (and then Ruby).

We are not quite done yet, but the basic structure is ready and needs
some tweaking for the perfect alignment of the triangles.
I am working on finding more cool one liners that are short enough for a sig.

Cool ... that should keep us busy for a while :)

-- shanko
 
J

James Edward Gray II

I came up with a cellular automata simulator but the input and rule
set took up too much space.

Subject change: In my spare time, I've been scanning the web for the
perfect simulation to do as a Ruby Quiz. I don't want to do Life like
everyone else does. We can do better.

So, if you can point me to a fun simulation (well described), I'll be
very grateful.

If you write it up and submit it as a Ruby Quiz, you'll be my hero.

Submit address: (e-mail address removed)

Sorry to derail the thread. We now return you to your regularly
scheduled discussion of Ruby signatures...

James Edward Gray II
 
M

Michael Neumann

Brian said:
Yes.

I am working on finding more cool one liners that are short enough for a sig.

actually a four liner...

ruby -rcomplex -e'c,m,w,h=Complex(-0.75,0.136),50,200,100;puts"P6\n"+\
"#{w} #{h}\n255";(0...h).each{|j|(0...w).each{|i|n,z=0,Complex(0.9*i/w,\
0.9*j/h);while n<=m&&(z-c).abs<=2;z=z*z+c;n+=1 end;print [20+n*10,0,\
rand*99].pack("C*")}}'|display

Regards,

Michael
 
G

Gavin Sinclair

ruby -rcomplex
-e'c,m,w,h=Complex(-0.75,0.136),50,200,100;puts"P6\n"+\
"#{w}
#{h}\n255";(0...h).each{|j|(0...w).each{|i|n,z=0,Complex(0.9*i/w,\
0.9*j/h);while n<=m&&(z-c).abs<=2;z=z*z+c;n+=1 end;print [20+n*10,0,\
rand*99].pack("C*")}}'|display

Running that on Cygwin, I get

display: unable to open X server `'.
-e:4:in `write': Broken pipe (Errno::EPIPE)
from -e:4:in `print'
from -e:4
from -e:2:in `each'
from -e:2
from -e:2:in `each'
from -e:2

Probably a platform issue, but I gotta say I'm a bit surprised.

Gavin
 
M

Michael Neumann

Gavin said:
ruby -rcomplex
-e'c,m,w,h=Complex(-0.75,0.136),50,200,100;puts"P6\n"+\
"#{w}
#{h}\n255";(0...h).each{|j|(0...w).each{|i|n,z=0,Complex(0.9*i/w,\
0.9*j/h);while n<=m&&(z-c).abs<=2;z=z*z+c;n+=1 end;print [20+n*10,0,\
rand*99].pack("C*")}}'|display


Running that on Cygwin, I get

display: unable to open X server `'.
-e:4:in `write': Broken pipe (Errno::EPIPE)
from -e:4:in `print'
from -e:4
from -e:2:in `each'
from -e:2
from -e:2:in `each'
from -e:2

Probably a platform issue, but I gotta say I'm a bit surprised.

Probably due to 'display'. The ruby code generates a .pnm image, which
is then displayed using 'display' (ImageMagick).

Regards,

Michael
 
G

Gavin Sinclair

Gavin said:
ruby -rcomplex
-e'c,m,w,h=Complex(-0.75,0.136),50,200,100;puts"P6\n"+\
"#{w}
#{h}\n255";(0...h).each{|j|(0...w).each{|i|n,z=0,Complex(0.9*i/w,\
0.9*j/h);while n<=m&&(z-c).abs<=2;z=z*z+c;n+=1 end;print [20+n*10,0,\
rand*99].pack("C*")}}'|display


Running that on Cygwin, I get

display: unable to open X server `'.
-e:4:in `write': Broken pipe (Errno::EPIPE)
from -e:4:in `print'
from -e:4
from -e:2:in `each'
from -e:2
from -e:2:in `each'
from -e:2

Probably a platform issue, but I gotta say I'm a bit surprised.
Probably due to 'display'. The ruby code generates a .pnm image, which
is then displayed using 'display' (ImageMagick).

But the code doesn't require rmagick, does it?

Gavin
 
M

Michael Neumann

Gavin said:
Gavin said:
On Monday, December 6, 2004, 10:43:50 PM, Michael wrote:



ruby -rcomplex
-e'c,m,w,h=Complex(-0.75,0.136),50,200,100;puts"P6\n"+\
"#{w}
#{h}\n255";(0...h).each{|j|(0...w).each{|i|n,z=0,Complex(0.9*i/w,\
0.9*j/h);while n<=m&&(z-c).abs<=2;z=z*z+c;n+=1 end;print [20+n*10,0,\
rand*99].pack("C*")}}'|display


Running that on Cygwin, I get

display: unable to open X server `'.
-e:4:in `write': Broken pipe (Errno::EPIPE)
from -e:4:in `print'
from -e:4
from -e:2:in `each'
from -e:2
from -e:2:in `each'
from -e:2

Probably a platform issue, but I gotta say I'm a bit surprised.

Probably due to 'display'. The ruby code generates a .pnm image, which
is then displayed using 'display' (ImageMagick).


But the code doesn't require rmagick, does it?

no it doesn't require rmagick. it generates the pnm image itself (and
this in 4 lines ;-).

The "display" command is there only to view the pnm image, oterhwise it
would print the image on STDOUT.

Regards,

Michael
 
O

Oliver Cromm

Brian said:
I am working on finding more cool one liners that are short enough
for a sig.

I came up with a cellular automata simulator but the input and
rule set took up too much space. So I will have to find something
else.... if not I will be sticking to my triangle signature.

You inspired me to another triangle one.
 
B

Brian Schröder

Oliver Cromm
ruby -e'require "Matrix";a=[1];(1..10).each{|n| print " "*(10-n)*3,a.map{|i|\
i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).to_a}'

You've got a type in there. It should be require "matrix" not require "Matrix".
(Or maybe it works on a windows system, because its case insensitive?)

Regards,

Brian
 
O

Oliver Cromm

Brian said:
Oliver Cromm said:
ruby -e'require "Matrix";a=[1];(1..10).each{|n| print " "*(10-n)*3,a.map{|i|\
i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).to_a}'

You've got a type in there. It should be require "matrix" not
require "Matrix". (Or maybe it works on a windows system, because
its case insensitive?)

Yes, it works on Windows, but I changed it for future use. Thanks.
 
T

trans. (T. Onoma)

On Tuesday 07 December 2004 06:12 pm, Oliver Cromm wrote:
| ruby -e'require "matrix";a=[1];(1..10).each{|n| print "
| "*(10-n)*3,a.map{|i|\
| i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).to_a}
|'

A touch smaller:

ruby -e'require"matrix";a=[1];(1..10).each{|n|print" "*(10-n)*3,a.map{|i|\
i.to_s.center(6)}.join,$/;a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'
 
M

Mauricio Fernández

On Tuesday 07 December 2004 06:12 pm, Oliver Cromm wrote:
| ruby -e'require "matrix";a=[1];(1..10).each{|n| print "
| "*(10-n)*3,a.map{|i|\
| i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).to_a}
|'

A touch smaller:

ruby -e'require"matrix";a=[1];(1..10).each{|n|print" "*(10-n)*3,a.map{|i|\
i.to_s.center(6)}.join,$/;a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'

Some obvious modifications:

ruby -rmatrix -le'a=[1];10.times{|n|print" "*(9-n)*3,"%3d "*a.size%a;a<<0;a=(
Vector[*a]+Vector[*a.reverse]).to_a}'

There's still potential for further optimization but I prefer to zzzZZ.
 
O

Oliver Cromm

Mauricio said:
On Tuesday 07 December 2004 06:12 pm, Oliver Cromm wrote:
| ruby -e'require "matrix";a=[1];(1..10).each{|n| print "
| "*(10-n)*3,a.map{|i|\
| i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).to_a}
|'

A touch smaller:

ruby -e'require"matrix";a=[1];(1..10).each{|n|print" "*(10-n)*3,a.map{|i|\
i.to_s.center(6)}.join,$/;a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'

Some obvious modifications:

ruby -rmatrix -le'a=[1];10.times{|n|print" "*(9-n)*3,"%3d "*a.size%a;a<<0;a=(
Vector[*a]+Vector[*a.reverse]).to_a}'

Optimal once you know the stuff - I'm learning!
There's still potential for further optimization but I prefer to zzzZZ.

a.size = n+1, better yet:

ruby -rmatrix -le'a=[1];1.upto(10){|n|print" "*(10-n)*3,"%3d "*n%a
a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'

I found the Vector stuff /kakkou ii/, but without, it's shorter.
Accepting the extra indent, my shortest is:

ruby -le'a=[];10.times{|n|a<<1;print" "*(9-n)*3,"%6d"*(n+1)%a
n.downto(1){|i|a+=a[i-1]}}'
 
O

Oliver Cromm

Mauricio said:
On Tuesday 07 December 2004 06:12 pm, Oliver Cromm wrote:
| ruby -e'require "matrix";a=[1];(1..10).each{|n| print "
| "*(10-n)*3,a.map{|i|\
| i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).to_a}
|'

A touch smaller:

ruby -e'require"matrix";a=[1];(1..10).each{|n|print" "*(10-n)*3,a.map{|i|\
i.to_s.center(6)}.join,$/;a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'

Some obvious modifications:

ruby -rmatrix -le'a=[1];10.times{|n|print" "*(9-n)*3,"%3d "*a.size%a;a<<0;a=(
Vector[*a]+Vector[*a.reverse]).to_a}'

Obvious once you know the stuff - I'm learning!
There's still potential for further optimization but I prefer to zzzZZ.

a.size = n+1, better yet:

ruby -rmatrix -le'a=[1];1.upto(10){|n|print" "*(10-n)*3,"%3d "*n%a
a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'

I found the Vector stuff /kakkou ii/, but without, it's shorter.
Accepting the extra indent, my shortest now is:

ruby -le'a=[];10.times{|n|a<<1;print" "*(9-n)*3,"%6d"*(n+1)%a
n.downto(1){|i|a+=a[i-1]}}'
 
T

trans. (T. Onoma)

| > On Wed, Dec 08, 2004 at 08:53:27AM +0900, trans. (T. Onoma) wrote:
| >> On Tuesday 07 December 2004 06:12 pm, Oliver Cromm wrote:
| >>| ruby -e'require "matrix";a=[1];(1..10).each{|n| print "
| >>| "*(10-n)*3,a.map{|i|\
| >>| i.to_s.center(6)}.join,$/;a.push(0);a=(Vector[*a]+Vector[*a.reverse]).t
| >>|o_a} '
| >>
| >> A touch smaller:
| >>
| >> ruby -e'require"matrix";a=[1];(1..10).each{|n|print"
| >> "*(10-n)*3,a.map{|i|\
| >> i.to_s.center(6)}.join,$/;a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'
| >
| > Some obvious modifications:
| >
| > ruby -rmatrix -le'a=[1];10.times{|n|print" "*(9-n)*3,"%3d
| > "*a.size%a;a<<0;a=( Vector[*a]+Vector[*a.reverse]).to_a}'
|
| Obvious once you know the stuff - I'm learning!
|
| > There's still potential for further optimization but I prefer to zzzZZ.
|
| a.size = n+1, better yet:
|
| ruby -rmatrix -le'a=[1];1.upto(10){|n|print" "*(10-n)*3,"%3d "*n%a
| a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'
|
| I found the Vector stuff /kakkou ii/, but without, it's shorter.
| Accepting the extra indent, my shortest now is:
|
| ruby -le'a=[];10.times{|n|a<<1;print" "*(9-n)*3,"%6d"*(n+1)%a
| n.downto(1){|i|a+=a[i-1]}}'

Amazing. Is any one making a collection of these?

T.
 
D

Dave Burt

trans. (T. Onoma) said:
On Wednesday 08 December 2004 12:52 am, Oliver Cromm wrote:
| ruby -le'a=[];10.times{|n|a<<1;print" "*(9-n)*3,"%6d"*(n+1)%a
| n.downto(1){|i|a+=a[i-1]}}'

Amazing. Is any one making a collection of these?


Check the thread [SOT]Signatures and one liners for more sigs. And there's a
duck one floating around, too.

Cheers,
Dave

i=1;loop{w=i&1==1?'v^':'^v';$><<"\e[2J\e[0;0H#{
' '*10}_\n #{i&1==1?' '*7+'<':'Quack! >'}(*)____,\n#{
' '*9}(` =~~/\n#{w*5}`---'#{w*4}";i+=sleep 1}
 
M

Mauricio Fernández

Some obvious modifications:

ruby -rmatrix -le'a=[1];10.times{|n|print" "*(9-n)*3,"%3d "*a.size%a;a<<0;a=(
Vector[*a]+Vector[*a.reverse]).to_a}'

Obvious once you know the stuff - I'm learning!
There's still potential for further optimization but I prefer to zzzZZ.

a.size = n+1, better yet:

Nice catch, we save 1 char.
ruby -rmatrix -le'a=[1];1.upto(10){|n|print" "*(10-n)*3,"%3d "*n%a
a<<0;a=(Vector[*a]+Vector[*a.reverse]).to_a}'

I found the Vector stuff /kakkou ii/, but without, it's shorter.

Yes, that's what I had in mind.
Accepting the extra indent, my shortest now is:

ruby -le'a=[];10.times{|n|a<<1;print" "*(9-n)*3,"%6d"*(n+1)%a
n.downto(1){|i|a+=a[i-1]}}'


ruby -e'a=[];10.times{|n|a<<1;puts" "*(9-n)*3+"%6d"*(n+1)%a;n.
times{|i|a[n]+=a[n-=1]}}'

It can be made to fit in a canonical 80-char line (actually 78):

a=[];10.times{|n|a<<1;puts" "*(9-n)*3+"%6d"*(n+1)%a;n.times{|i|a[n]+=a[n-=1]}}

The indentation code might allow further optimization.
 
F

Florian Gross

Mauricio said:
It can be made to fit in a canonical 80-char line (actually 78):

a=[];10.times{|n|a<<1;puts" "*(9-n)*3+"%6d"*(n+1)%a;n.times{|i|a[n]+=a[n-=1]}}

Small one: (two more chars)
a=[];10.times{|n|a<<1;puts" "*(9-n)*3+"%6d"*-~n%a;n.times{|i|a[n]+=a[n-=1]}}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top