[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

P2P application in 15 lines of Python posted on slashdot

slonik AZ

12/15/2004 11:41:00 PM

Hi Everybody,
slashdot published an article on someone's
15 lines long Peer-2-Peer application
http://developers.slashdot.org/article.pl?sid=04/12/...

Another person followed up with a 9 line equivalent Perl code.

I wonder what an equivalent Ruby program would look like?

I do not care whether it 15 or 25 lines. Python program is not very readable.
Can Ruby variant be concise, yet more clear?

--Leo--


17 Answers

Giovanni Intini

12/15/2004 11:54:00 PM

0

I was going to post the same message :)
Good rubyists unite! (I'm not good :( ). Let's write a short more
understandable program!


Purple Meteor

12/16/2004 1:11:00 AM

0

On Thu, 16 Dec 2004 08:53:49 +0900, Giovanni Intini <intinig@gmail.com> wrote:
> Let's write a short more understandable program!
Actually, someone answered with a web server written in only 3 lines of codes.
More info here: http://tinyurl...


Michael DeHaan

12/16/2004 1:20:00 AM

0

LOL. The Python example used quite a few core libraries, so I think
a one-line instantiation of WebBRICK would suffice for a web server
example :)

Seriously, these golf competitions don't do much for me. Show me
formatted code that applies some interesting algorithms instead.
That's how you spar with other languages!

On Thu, 16 Dec 2004 10:11:15 +0900, Purple Meteor
<purple.meteor@gmail.com> wrote:
> On Thu, 16 Dec 2004 08:53:49 +0900, Giovanni Intini <intinig@gmail.com> wrote:
> > Let's write a short more understandable program!
> Actually, someone answered with a web server written in only 3 lines of codes.
> More info here: http://tinyurl...
>
>


gabriele renzi

12/16/2004 7:09:00 AM

0

Michael DeHaan ha scritto:
> LOL. The Python example used quite a few core libraries, so I think
> a one-line instantiation of WebBRICK would suffice for a web server
> example :)

ruby -rwebrick -e 'WEBrick::HTTPServer.new(:DocumentRoot=>".").start'
but it is a oneliner in python too ;)

> Seriously, these golf competitions don't do much for me. Show me
> formatted code that applies some interesting algorithms instead.
> That's how you spar with other languages!


def qs(l)
return [] if (x,*xs=*l).empty?
less, more = xs.partition{|y| y < x}
qs(less) + [x] + qs(more)
end

algoritm suggestions are welcome :)

Nikolai Weibull

12/16/2004 9:52:00 AM

0

* gabriele renzi <rff_rff@remove-yahoo.it> [Dec 16, 2004 10:30]:
> def qs(l)
> return [] if (x,*xs=*l).empty?
> less, more = xs.partition{|y| y < x}
> qs(less) + [x] + qs(more)
> end

> algoritm suggestions are welcome :)

How about

def qs(l)
l.sort!
end

;-),
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Florian Gross

12/16/2004 11:39:00 AM

0

slonik AZ wrote:

> slashdot published an article on someone's
> 15 lines long Peer-2-Peer application
> http://developers.slashdot.org/article.pl?sid=04/12/...
>
> Another person followed up with a 9 line equivalent Perl code.
>
> I wonder what an equivalent Ruby program would look like?

I did this 9.5 hours ago. Compared to the python one it is not
vulnerable to File stealing attacks (a client can request a file
.../foobar and ~/foobar from the python server and will get it back
AFAIK) and 6 lines long. It is however vulnerable to the DRb style
..instance_eval exploits. I will fix this shortly, but I might have to
use 7 lines then.
# Server: ruby p2p.rb password server server-uri merge-servers
# Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337
# Client: ruby p2p.rb password client server-uri download-pattern
# Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
require'drb';F,D,C,P,M,U,*O=File,Class,Dir,*ARGV;def s(p)F.split(p[/[^|].*/])[-1
]end;def c(u);DRbObject.new((),u)end;def x(u)[P,u].hash;end;M=="client"&&c(U).f(
x(U)).each{|n|p,c=x(n),c(n);(c.f(p,O[0],0).map{|f|s f}-D["*"]).each{|f|F.open(f,
"w"){|o|o<<c.f(p,f,1)}}}||(DRb.start_service U,C.new{def f(c,a=[],t=2)c==x(U)&&(
t==0&&D[s(a)]||t==1&&F.read(s(a))||p(a))end;def y()(p(U)+p).each{|u|c(u).f(x(u),
p(U))rescue()};self;end;private;def p(x=[]);O.push(*x).uniq!;O;end}.new.y;sleep)

Brian Schröder

12/16/2004 12:08:00 PM

0

On Thu, 16 Dec 2004 18:51:38 +0900
Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:

> * gabriele renzi <rff_rff@remove-yahoo.it> [Dec 16, 2004 10:30]:
> > def qs(l)
> > return [] if (x,*xs=*l).empty?
> > less, more = xs.partition{|y| y < x}
> > qs(less) + [x] + qs(more)
> > end
>
> > algoritm suggestions are welcome :)
>
> How about
>
> def qs(l)
> l.sort!
> end
>
> ;-),
> nikolai
>

I hope sort is not implemented as quicksort ;)

Regards,

Brian


--
Brian Schröder
http://www.brian-sch...



Nikolai Weibull

12/16/2004 1:17:00 PM

0

* Brian Schröder <ruby@brian-schroeder.de> [Dec 16, 2004 13:10]:
> > How about

> > def qs(l)
> > l.sort!
> > end

> I hope sort is not implemented as quicksort ;)

array.c:

static VALUE
sort_internal(ary)
VALUE ary;
{
qsort(RARRAY(ary)->ptr, RARRAY(ary)->len, sizeof(VALUE),
rb_block_given_p()?sort_1:sort_2);
return ary;
}

rb_ary_sort_bang(ary)
VALUE ary;
{
rb_ary_modify(ary);
if (RARRAY(ary)->len > 1) {
FL_SET(ary, ARY_TMPLOCK); /* prohibit modification during sort */
rb_ensure(sort_internal, ary, sort_unlock, ary);
}
return ary;
}

Just hope your systems qsort() is any good,
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


ts

12/16/2004 1:29:00 PM

0

>>>>> "N" == Nikolai Weibull <mailing-lists.ruby-talk@rawuncut.elitemail.org> writes:

N> Just hope your systems qsort() is any good,

uln% grep qsort util.h
void ruby_qsort _((void*, const int, const int, int (*)(), void*));
#define qsort(b,n,s,c,d) ruby_qsort(b,n,s,c,d)
uln%


Guy Decoux


Florian Gross

12/16/2004 1:33:00 PM

0

Florian Gross wrote:

> slonik AZ wrote:
>
>> slashdot published an article on someone's
>> 15 lines long Peer-2-Peer application
>> http://developers.slashdot.org/article.pl?sid=04/12/...
>>
>> Another person followed up with a 9 line equivalent Perl code.
>>
>> I wonder what an equivalent Ruby program would look like?
>
> I did this 9.5 hours ago. Compared to the python one it is not
> vulnerable to File stealing attacks (a client can request a file
> ../foobar and ~/foobar from the python server and will get it back
> AFAIK) and 6 lines long. It is however vulnerable to the DRb style
> .instance_eval exploits. I will fix this shortly, but I might have to
> use 7 lines then.

Here we go. Thanks to Mauricio Fernández for helping out with cutting
off a few important characters!

#!/usr/bin/ruby
# Server: ruby p2p.rb password server server-uri merge-servers
# Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337
# Client: ruby p2p.rb password client server-uri download-pattern
# Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
require'drb';F,D,P,M,U,*O=File,Dir,*ARGV;def s(p)F.basename p[/\w.*/]end;def c u
DRbObject.new((),u)end;def x(u);[P,u].hash;end;M["c"]?c(U).f(x(U)).map{|n|p=x(n)
c=c(n);(c.f(p,O[0],0).map{|f|s f}-D["*"]).map{|f|open(f,"w")<<c.f(p,f,1)}}:(DRb.
start_service U,Class.new{def p(z=O)O.push(*z).uniq!;O;end;new.methods.map{|m|m[
/_[_t]/]||private(m)};def f(c,a=[],t=2)c==x(U)&&(t==0?D[s(a)]:t==1?F.read(s(a)):
p(a))end;def y;(p(U)+p).map{|u|c(u).f(x(u),p(U))rescue()};self;end}.new.y;sleep)