Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [C&C] Re: [tlug] OT: interesting NY times article:High-Tech Japanese, Running Out of Engineers



On Wed, May 28, 2008 at 03:03:43AM +0900, Stephen J. Turnbull wrote:
> Dave Brown writes:
>  > On Tue, May 27, 2008 at 03:05:17AM +0900, Stephen J. Turnbull wrote:
>  > > David J Iannucci writes:
>  > > 
>  > >  > Much as I love Perl, I must admit that the concise beauty of the way
>  > >  > Ruby applies the string transformation without the side-effect takes
>  > >  > the cake :-)
>  > > 
>  > > Hm?  Perl uses the editable buffer model, Ruby has immutable strings,
>  > > Emacs provides both.  Dif'rent strokes.
>  > 
>  > I think you're confusing Ruby and Python there.  Ruby's strings are as
>  > editable as you like.
> 
> No, I'm inferring it from David I's comment about lack of side effect.
> Does Ruby also provide a method with the same signature that mutates
> the string in-place?

For certain.  You just have to be more emphatic and and use #sub!
instead of #sub.

>  > (Symbols are immutable, but it would make no sense for them to be
>  > anything else.)
> 
> Actually, I've seen code in Lisp that mutates symbol names.  The
> symbols are gensyms (ie, uninterned) whose values are parse trees and
> the like, and the idea was to provide an indication of the state from
> the pname.  Sort of like the way many programs manipulate argv[0].
> 
> Now, I don't say this makes sense to sane and/or earthly programmers,
> but you have to admit there's a semblance of internal logic there.
> Dave?  Dave?  Oh, was it "ettiquette time"?  At least you didn't
> spontaneously combust!

I only have a *mild* headache.  It vaguely reminds me of editing my
Commodore 64's BASIC interpreter so that FOR was now called CAT or
something.

I didn't get out much as a kid, why do you ask?

> P.S.  "As editable as I like?"  You dare say that to an Emacs user?
> Prove it!

I'll follow the adventures of a variable called "x" (which I use because
I once encountered a book on programming that said that "x" is the very
last name you should use for anything other than a coordinate) for a bit
for you then.

    irb(main):001:0> x = "foo"
    => "foo"
    irb(main):002:0> x[0] = "bar"
    => "bar"
    irb(main):003:0> x
    => "baroo"

The String#[] method can also take two arguments.

    irb(main):004:0> x[3,0] = "quux"
    => "quux"
    irb(main):005:0> x
    => "barquuxoo"

You can use sub! and gsub! to edit strings.  The second is global search
and replace.

    irb(main):006:0> x.sub! /[aeiou]/, "honk"
    => "bhonkrquuxoo"
    irb(main):007:0> x.gsub! /[aeiou][^aeiou]/, "BANG"
    => "bhBANGkrquBANGoo"

You can use different classes of argument for String#[], like a range,
another string, or a regex.

    irb(main):008:0> x[5..99]="hurk"
    => "hurk"
    irb(main):009:0> x
    => "bhBANhurk"
    irb(main):009:0> x["hurk"] = "fred"
    => "bhBANfred"
    irb(main):010:0> x[/[A-Z]+/] = "EXCLAMATION"
    => "bhEXCLAMATIONfred"

And there are also a handful of alphabetic-manipulation methods:

    irb(main):011:0> x.downcase!
    => "bhexclamationfred"
    irb(main):011:0> x.capitalize!
    => "Bhexclamationfred"
    irb(main):012:0> x.upcase!
    => "BHEXCLAMATIONFRED"

Plus a bunch of others, of course.

--Dave


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links