[Chocolate Ketchup Dressing] A working Ruby source code filter

Moin!

I've implemented a Ruby source code filter for the frequently requested ".=" operator. (Replace by result of method call)

Here's a boring example of how it is used:

#!/usr/bin/ruby -rfilter
obj = "foobar"
obj .= reverse
p obj # => "raboof"

I'm using IRB's Ruby lexer for this so it should in theory not screw you up as badly as the Switch.pm module for Perl 5.

Details and source code are available at http://www.codepaste.org/view/paste/300 -- but the source code and the Binding.of_caller dependency are also attached to this mail.

Maybe this technique can be used for trying out other, more complex modifications of the Ruby language?

Kind regards,
Florian Gross

filter.rb (4.08 KB)

binding_of_caller.rb (2.56 KB)

Florian Gross ha scritto:

Moin!

I've implemented a Ruby source code filter for the frequently requested ".=" operator. (Replace by result of method call)

hypercool!

Maybe this technique can be used for trying out other, more complex modifications of the Ruby language?

very interesting.. you rock :slight_smile:

Hi, Florian. How does .= differ from bang methods?
I mean obj.reverse seems to be the same as obj.reverse!

Thanks and kind regards,
Ed

···

On Sat, 16 Oct 2004 09:34:28 +0900, Florian Gross <flgr@ccan.de> wrote:

Moin!

I've implemented a Ruby source code filter for the frequently requested
".=" operator. (Replace by result of method call)

Here's a boring example of how it is used:

#!/usr/bin/ruby -rfilter
obj = "foobar"
obj .= reverse
p obj # => "raboof"

--
We all know life is often unfair. The trick is to make it unfair in your favor.

"Edgardo Hames" <ehames@gmail.com> schrieb im Newsbeitrag
news:478c16ae041018053737c72bec@mail.gmail.com...

> Moin!
>
> I've implemented a Ruby source code filter for the frequently

requested

> ".=" operator. (Replace by result of method call)
>
> Here's a boring example of how it is used:
>
> #!/usr/bin/ruby -rfilter
> obj = "foobar"
> obj .= reverse
> p obj # => "raboof"

Hi, Florian. How does .= differ from bang methods?
I mean obj.reverse seems to be the same as obj.reverse!

Not quite: the bang method usually saves an intermediate instance *and* it
might have undesirable side effects (if someone else holds a reference to
the instance and doesn't expect it to be modified). As a consequence,
".=" will work with frozen objects, while bang methods usually don't. As
usual it's a tradeoff / design decision which of both approaches you use.

Kind regards

    robert

···

On Sat, 16 Oct 2004 09:34:28 +0900, Florian Gross <flgr@ccan.de> wrote: