Daniel Brockman wrote:
>Wow, I really like it! Why didn't I think of that?
>
>
You weren't being challenged by someone showing you a code snippet
claiming that APL is a superior language. :) BTW, change *b to b, in my
code.
>This *so* needs to be put in the standard library. :-)
>
>
Aww... that's the nicest thing anybody could ever say...
>Way cool! I actually like this one, and I'm not kidding.
>Redefine Array#* to double-dispatch on symbols and this will
>actually be usable. Think about itĄ˝
>
> [1,2,3] * "+" means [1,2,3].join("+"),
>
>so it makes sense for
>
> [1,2,3] * :+ to mean [1,2,3].inject(&:+).
>
>
Actually, I like that a lot more than :+ * [1,2,3].
>>(I would've overloaded /, but Ruby kept thinking I was
>>trying to construct a regular expression.)
>>
>>
>Hmm, what would you have had that do?
>
>
Sorry, I just assumed that everybody except me knew APL, so I could just
talk about it as if I knew what I was talking about and get buy-in from
everybody else. In APL, the syntax for doing that is something akin to
"+ / 1 2 3". But you showed an excellent reason why "*" makes more sense
for Ruby.
>Wow... amazing. Of course, this
>
> ["Hello!", [/Hell/, "Good"], [/o!/, "bye."]] ** :gsub
>
>is both longer and more cryptic than this,
>
> "Hello!".gsub(/Hell/, "Good").gsub(/o!/, "bye.")
>
>but still... the former has *no* duplication. :-)
>
>
Well, if you're doing, like, seven gsubs, the former will win out, but
if you're chaining seven gsubs, you're probably doing something wrong. I
suppose you could use "** :gsub" to aid in some sort of configuration
file thing:
class Array
def **(sym)
inject {|a,b| a.send(sym,*b)} end end
gsubs = YAML::load [[/Hell/,"Good"],[/o!/,"bye."]].to_yaml #your config
file here
some_words_ive_got_lying_around = %w{cat dog tree Hello! french oreo!}
some_words_ive_got_lying_around.map! {|word| [word,*gsubs] ** :gsub }
#=> ["cat", "dog", "tree", "Goodbye.", "french", "orebye."]
>By the way, the above example suggests that it might be
>useful to have String#gsub take multiple arguments:
>
> class String
> unless method_defined? :gsub1
>
>
Heh, never seen that "unless method_defined?" thing before. I like it, I
think. (I'm still not comfortable about the alias-and-call-original
trick, and this makes me a little more comfortable about it.)
> "Hello!".gsub(/Hell/ => "Good", /o!/ => "bye.") #=> "Goodbye."
> "Hello!".gsub(/Hell/, "Good", /o!/, "bye.") #=> "Goodbye."
>
>
Hehe, neat. I actually haven't used gsub a lot, so I can't comment on
its usefulness, but I see a lot of other people gsubbing a lot, so I bet
it would be a pretty desired addition.
>Anyway, thanks for sharing your cool inventions, Devin. :-)
>
>
You're welcome, and thanks for calling my inventions cool! Makes me feel
better for not contributing a library or toolkit or whatever to the Ruby
community. :)
Random aside: I learned something about #map just now:
irb(main):001:0> [1,2,3].map
=> [1, 2, 3]
irb(main):002:0> [1,2,3].map!
LocalJumpError: no block given
from (irb):2:in `map!'
from (irb):2
Odd... :P
Devin