Building a REPL for Ruby

Yes, I know ruby already has irb, but I have a friend who keeps engaging me in these python vs. ruby wars and he recently submitted the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less. Furthermore, it can't be written in ruby."

I was thinking of using Ruby/C but I've never wrote anything like this before. Is their any tips anyone can give me so I can uphold ruby's honor?

-Thanks
Shalev

Shalev NessAiver wrote:

Yes, I know ruby already has irb, but I have a friend who keeps
engaging me in these python vs. ruby wars and he recently submitted
the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less.
Furthermore, it can't be written in ruby."

Either:

15:08:08 [source]: ruby -r irb -e 'IRB.start'
irb(main):001:0> 1+10
=> 11
irb(main):002:0> exit

Or:

16:19:51 [source]: ruby -e '$bnd = binding; while (line=gets); exit if
/exit/ =~ line; p eval(line,$bnd); end'
1+10
11
a="foo"
"foo"
a << "bar"
"foobar"
a
"foobar"
exit
16:20:17 [source]:

You see, it's a one liner - either way. :slight_smile:

Admittedly the second one doesn't cope with multiline input. But I guess
it doesn't take too much to add that.

I was thinking of using Ruby/C but I've never wrote anything like
this before. Is their any tips anyone can give me so I can uphold
ruby's honor?

Dunno whether you actually have to enter this competition: if he doesn't
like Ruby, let him continue using Python...

Kind regards

    robert

Shalev NessAiver schrieb:

Yes, I know ruby already has irb, but I have a friend who keeps engaging me in these python vs. ruby wars and he recently submitted the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less. Furthermore, it can't be written in ruby."

I was thinking of using Ruby/C but I've never wrote anything like this before. Is their any tips anyone can give me so I can uphold ruby's honor?

Maybe not fully featured, but it's a start:

   loop do
     print "=> "
     puts( eval( gets ) )
   end

What features are you and your friend looking for?

Regards,
Pit

Can he write a Python REPL in less than 70 lines?

···

On 8/15/05, Shalev NessAiver <shalev@simplyphysics.com> wrote:

Yes, I know ruby already has irb, but I have a friend who keeps
engaging me in these python vs. ruby wars and he recently submitted
the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less.
Furthermore, it can't be written in ruby."

I was thinking of using Ruby/C but I've never wrote anything like
this before. Is their any tips anyone can give me so I can uphold
ruby's honor?

-Thanks
Shalev

--
Bill Atkins

In article <D20F7DFA-6B12-46A1-8F7C-8407045F9A8E@simplyphysics.com>,

Yes, I know ruby already has irb, but I have a friend who keeps
engaging me in these python vs. ruby wars and he recently submitted
the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less.
Furthermore, it can't be written in ruby."

'scuse my ignorance, but what's a REPL?

I was thinking of using Ruby/C but I've never wrote anything like
this before. Is their any tips anyone can give me so I can uphold
ruby's honor?

I guess I'm really out of it here, but how can writing something in a
language other than Ruby uphold Ruby's 'honor'?

Phil

···

Shalev NessAiver <shalev@simplyphysics.com> wrote:

I guess he means write it in C then?

Maybe I misunderstand the problem, but here's one that's exactly 70 lines:

/**
* \file repl.c

···

On Monday 15 August 2005 10:11, Shalev NessAiver wrote:

Yes, I know ruby already has irb, but I have a friend who keeps
engaging me in these python vs. ruby wars and he recently submitted
the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less.
Furthermore, it can't be written in ruby."

*
* \author Ben Giddings <bg-ruby@infofiend.com>
* Copyright (C) 2005, Ben Giddings
*
*/

#include <stdio.h>
#include <ruby.h>

#define INPUT_SIZE 0x100

int main(int argc, char **argv)
{
    char input_buff[INPUT_SIZE];
    int error;

    ruby_init();
    ruby_script("repl"); /* ? */

    while (1)
    {
        memset(input_buff, '\0', sizeof(input_buff));
        printf("repl> "); // no newline on purpose
        fgets(input_buff, INPUT_SIZE-1, stdin);
        if (0 == strncmp("quit", input_buff, 4))
        {
            break;
        }
        rb_p(rb_eval_string_protect(input_buff, &error));
        if (error)
        {
            rb_p(ruby_errinfo);
        }
    }

    return error;
}

/*
Hmm, line 42. Guess I gotta add another 28 lines to get to 70

He addresses a man standing behind a desk marked "Complaints".
C: I wish to complain, British-Railways Person.
Attendant: I DON'T HAVE TO DO THIS JOB, YOU KNOW!!!
C: I beg your pardon...?
A: I'm a qualified brain surgeon! I only do this job because I like being
my own boss!
C: Excuse me, this is irrelevant, isn't it?
A: Yeah, well it's not easy to pad these python files out to 200 lines, you
know.
C: Well, I wish to complain. I got on the Bolton train and found myself
deposited here in Ipswitch.
A: No, this is Bolton.
C: (to the camera) The pet shop man's brother was lying!!
A: Can't blame British Rail for that.
C: In that case, I shall return to the pet shop!
He does.
C: I understand this IS Bolton.
O: (still with the fake mustache) Yes?
C: You told me it was Ipswitch!
O: ...It was a pun.
C: (pause) A PUN?!?
O: No, no...not a pun...What's that thing that spells the same backwards as
forwards?
C: (Long pause) A palindrome...?
O: Yeah, that's it!
C: It's not a palindrome! The palindrome of "Bolton" would be "Notlob"!! It
don't work!!
O: Well, what do you want?
C: I'm not prepared to pursue my line of inquiry any longer as I think this
is getting too silly!
Sergeant-Major: Quite agree, quite agree, too silly, far too silly...
*/

And also not in Python? sheesh. This is the worlds strangest challenge.

···

On 8/15/05, Bill Atkins <batkins57@gmail.com> wrote:

Can he write a Python REPL in less than 70 lines?

On 8/15/05, Shalev NessAiver <shalev@simplyphysics.com> wrote:
> Yes, I know ruby already has irb, but I have a friend who keeps
> engaging me in these python vs. ruby wars and he recently submitted
> the challenge to me to
>
> "Write a fully featured REPL for ruby in 70 lines or less.
> Furthermore, it can't be written in ruby."
>
> I was thinking of using Ruby/C but I've never wrote anything like
> this before. Is their any tips anyone can give me so I can uphold
> ruby's honor?
>
> -Thanks
> Shalev
>
>

--
Bill Atkins

Phil Tomson wrote:

...
'scuse my ignorance, but what's a REPL?

Read Eval Print Loop

I was thinking of using Ruby/C but I've never wrote anything like this before. Is their any tips anyone can give me so I can uphold ruby's honor?

I guess I'm really out of it here, but how can writing something in a language other than Ruby uphold Ruby's 'honor'?

Putting aside the general idea that any deep concern over upholding "honor" for some a programming language suggests a certain irrationality, I'll offer a guess:

"Language Foo is so complex|convoluted|inconsistent that writing a Foo REPL is an inherently non-trivial task when using any language other than Foo (which may already include REPL-friendly functions, such as 'eval()' )."

The flip side proves nothing; it is easy to define trivial languages such that a REPL may be written in near-zero lines of code. Does ease of REPL-witting indicate concise, clear language design, or show that the language is merely simplistic?

Anyway, as for those who do not care for Ruby, I'd say let 'em be; not using Ruby is punishment enough.

James Britt

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

Phil Tomson ha scritto:

In article <D20F7DFA-6B12-46A1-8F7C-8407045F9A8E@simplyphysics.com>,

Yes, I know ruby already has irb, but I have a friend who keeps engaging me in these python vs. ruby wars and he recently submitted the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less. Furthermore, it can't be written in ruby."

'scuse my ignorance, but what's a REPL?

Read-Eval-Print-Loop. Basically something similar to our IRB, or the interactive prompt of python or of lisp environments (and many others) where you write stuff and that is immediately executed giving you the result

···

Shalev NessAiver <shalev@simplyphysics.com> wrote:

Yes. Evidently he's written a REPL for python in C (using the python headers of course) in 70 lines.
Really, I didn't quite understand his challenge, which is part of why I posed this question to the ML.

I think his point is to see how easy it is to see how well ruby interoperates with other languages.

Now that I've read this thread and considered it more, I think he just doesn't get it about ruby and nothing
I can do is going to convince him otherwise. Only thing is he called me a VBStudio scripter when I said I
used rails, which annoyed me enough to start thinking about this in the first place :slight_smile:

Nevertheless, I think it would still be an interesting thing to attempt... if only to help me learn the C side of
Ruby better.

-Thanks for the suggestions
Shalev

···

On Aug 15, 2005, at 10:44 AM, Bill Atkins wrote:

Can he write a Python REPL in less than 70 lines?

On 8/15/05, Shalev NessAiver <shalev@simplyphysics.com> wrote:

Yes, I know ruby already has irb, but I have a friend who keeps
engaging me in these python vs. ruby wars and he recently submitted
the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less.
Furthermore, it can't be written in ruby."

I was thinking of using Ruby/C but I've never wrote anything like
this before. Is their any tips anyone can give me so I can uphold
ruby's honor?

-Thanks
Shalev

--
Bill Atkins

Phil Tomson wrote:

'scuse my ignorance, but what's a REPL?

<Phew! Someone else asked first!>

···

--
Chris Game

"You must be an intellectual. No normal person would say a
thing like that." -- George Orwell

Forgive my newbness, but how would one use/compile that code?
The instructions for creating an extension (using extconf.rb) seems to be geared towards
compiling Ruby-C classes... how I just compile and run this code?

-Thanks
Shalev

···

On Aug 16, 2005, at 2:08 PM, Ben Giddings wrote:

On Monday 15 August 2005 10:11, Shalev NessAiver wrote:

Yes, I know ruby already has irb, but I have a friend who keeps
engaging me in these python vs. ruby wars and he recently submitted
the challenge to me to

"Write a fully featured REPL for ruby in 70 lines or less.
Furthermore, it can't be written in ruby."

I guess he means write it in C then?

Maybe I misunderstand the problem, but here's one that's exactly 70 lines:

/**
* \file repl.c
*
* \author Ben Giddings <bg-ruby@infofiend.com>
* Copyright (C) 2005, Ben Giddings
*
*/

#include <stdio.h>
#include <ruby.h>

#define INPUT_SIZE 0x100

int main(int argc, char **argv)
{
    char input_buff[INPUT_SIZE];
    int error;

    ruby_init();
    ruby_script("repl"); /* ? */

    while (1)
    {
        memset(input_buff, '\0', sizeof(input_buff));
        printf("repl> "); // no newline on purpose
        fgets(input_buff, INPUT_SIZE-1, stdin);
        if (0 == strncmp("quit", input_buff, 4))
        {
            break;
        }
        rb_p(rb_eval_string_protect(input_buff, &error));
        if (error)
        {
            rb_p(ruby_errinfo);
        }
    }

    return error;
}

/*
Hmm, line 42. Guess I gotta add another 28 lines to get to 70

He addresses a man standing behind a desk marked "Complaints".
C: I wish to complain, British-Railways Person.
Attendant: I DON'T HAVE TO DO THIS JOB, YOU KNOW!!!
C: I beg your pardon...?
A: I'm a qualified brain surgeon! I only do this job because I like being
my own boss!
C: Excuse me, this is irrelevant, isn't it?
A: Yeah, well it's not easy to pad these python files out to 200 lines, you
know.
C: Well, I wish to complain. I got on the Bolton train and found myself
deposited here in Ipswitch.
A: No, this is Bolton.
C: (to the camera) The pet shop man's brother was lying!!
A: Can't blame British Rail for that.
C: In that case, I shall return to the pet shop!
He does.
C: I understand this IS Bolton.
O: (still with the fake mustache) Yes?
C: You told me it was Ipswitch!
O: ...It was a pun.
C: (pause) A PUN?!?
O: No, no...not a pun...What's that thing that spells the same backwards as
forwards?
C: (Long pause) A palindrome...?
O: Yeah, that's it!
C: It's not a palindrome! The palindrome of "Bolton" would be "Notlob"!! It
don't work!!
O: Well, what do you want?
C: I'm not prepared to pursue my line of inquiry any longer as I think this
is getting too silly!
Sergeant-Major: Quite agree, quite agree, too silly, far too silly...
*/

Logan Capaldo wrote:

...

And also not in Python? sheesh. This is the worlds strangest challenge.

It makes me think that the "... and not in Ruby" clause is there because he can't write a REPL for Python in less than 70 lines of Python.

And what would this demonstrate about either Ruby or Python?

That Python syntax is friendlier for machines than for humans, so a REPL is easier to write?

James

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

On a related note,

Is there a bash like shell written for administering linux/windows in ruby ?
Something like monad ? To give a stupid example, we could wrap 'man' in
a class and add a 'search' method which would call 'man -k'.

Just thought ruby could be perfect for something like this ...

···

On 8/15/05, gabriele renzi <surrender_it@remove-yahoo.it> wrote:

Phil Tomson ha scritto:
> In article <D20F7DFA-6B12-46A1-8F7C-8407045F9A8E@simplyphysics.com>,
> Shalev NessAiver <shalev@simplyphysics.com> wrote:
>
>>Yes, I know ruby already has irb, but I have a friend who keeps
>>engaging me in these python vs. ruby wars and he recently submitted
>>the challenge to me to
>>
>>"Write a fully featured REPL for ruby in 70 lines or less.
>>Furthermore, it can't be written in ruby."
>
>
>
> 'scuse my ignorance, but what's a REPL?

Read-Eval-Print-Loop. Basically something similar to our IRB, or the
interactive prompt of python or of lisp environments (and many others)
where you write stuff and that is immediately executed giving you the result

--
Reyn Vlietstra

Shalev NessAiver wrote:

I think his point is to see how easy it is to see how well ruby interoperates with other languages.

> Only thing is he called

me a VBStudio scripter when I said I
used rails, which annoyed me enough to start thinking about this in the first place :slight_smile:

Well, for what it's worth: I'm a former Pythonista, now a 100% Rubyist.

I have recently integrated a C++ financial/statistical application as a Ruby extension (DLL) using swig, and I did it with writing a simple four line script, and modifying a makefile a couple of places!

The easiest thing in the world!

Now, I have also formerly written Python extensions in C, and it was not a walk in the park!

Tell your 'friend' that Ruby is just a much better designed language (I would know, having used Python for several years), and integrating Ruby with C/C++ is a breeze!

Baalbek

Shalev NessAiver wrote:
..

Now that I've read this thread and considered it more, I think he just doesn't get it about ruby and nothing
I can do is going to convince him otherwise. Only thing is he called me a VBStudio scripter when I said I
used rails, which annoyed me enough to start thinking about this in the first place :slight_smile:

Well, comparisons between Rails (with AJAX) and Visual Basic are not entirely misplaced.

But I suspect there was some envy involved.

James

···

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys

the wisdom of the week
->> my new signature

thanks James

···

On Tue, 16 Aug 2005 02:26:52 +0900 James Britt <james_b@neurogami.com> wrote:

not using Ruby is punishment enough.

Forgive my newbness, but how would one use/compile that code?
The instructions for creating an extension (using extconf.rb) seems
to be geared towards
compiling Ruby-C classes... how I just compile and run this code?

For me, under linux, it was:

gcc -I /usr/lib/ruby/1.8/i686-linux -lm -ldl -lcrypt -lruby -o repl repl.c

If you're not a C programmer that's "compile, look for headers
in /usr/lib/ruby/1.8/i686-linux (that's where ruby.h lives), dynamically
link against the math (m), dl, crypt, and ruby libraries, name the output
file 'repl' and use repl.c as the source file."

If you want to distribute it to another system that might not have Ruby
installed, you can also statically link against Ruby:

gcc -I /usr/lib/ruby/1.8/i686-linux -lm -ldl -lcrypt -o repl
repl.c /usr/lib/libruby18-static.a

The one thing to be aware of is that, unlike IRB, this one is single-line
only, so to define a function you'd have to do:

def foo; puts "Foo!"; end

if you just type "def foo" and hit enter, it will decide it's a syntax
error. I guess the real "IRB" interpretation is able to distinguish
between syntax errors and incomplete entry? I dunno.

Ben

···

On Tuesday 16 August 2005 19:01, Shalev NessAiver wrote:

James Britt ha scritto:

Logan Capaldo wrote:

...
And also not in Python? sheesh. This is the worlds strangest challenge.

It makes me think that the "... and not in Ruby" clause is there because he can't write a REPL for Python in less than 70 lines of Python.

I guess that writing a python repl in few lines would actually be quite hard, due to the fact that they have different concepts for statements and expressions.
The latter can be done at runtime with the "eval(string)" function, while the former need the "exec string" statement (which in turn can't be used in an expression..)

My bet is that he thought ruby had this difference, too :wink:

And what would this demonstrate about either Ruby or Python?
That Python syntax is friendlier for machines than for humans, so a REPL is easier to write?

that the statement/expression separtion is ugly, imho :wink: