ruby %w equivalent

P

Piet van Oostrum

A> Why would they want to make such an obscure API ? ... didn't they have
A> Python to learn from (I am truly amazed - nothing cynical ...just ...
A> why ?!!!!)

I think it is modeled after Perl.
 
A

Antoine De Groote

Thorsten said:
* Antoine De Groote (Tue, 26 Sep 2006 12:06:38 +0200)
Thorsten said:
* John Machin (24 Sep 2006 15:32:20 -0700)
Antoine De Groote wrote:
is there a python equivalent for the ruby %w operator?
%w{a b c} creates an array with strings "a", "b", and "c" in ruby...

| >>> "a b c".split()
| ['a', 'b', 'c']

... appears to match your single example.
Something wrong with "list('abc')"? Or is it too simple?!

Thorsten
As far as I can tell this works for single characters only. You're not
able to split words, as in "one two three".split().

It does satisfy your example in your first posting nevertheless.

Thorsten

Absolutely :)
 
N

Nick Craig-Wood

Duncan Booth said:
Nick Craig-Wood said:
In python when making __slots__ or module.__all__ you end up typing
lists of objects or methods and they turn out like this which is quite
a lot of extra typing

__slots__ = ["method1", "method2", "method3", "method4", "method5"]

For __all__ you can use a decorator to avoid retyping the function name at
all. e.g.

def public(f):
all = f.func_globals.setdefault('__all__', [])
all.append(f.__name__)
return f

@public
def foo(): pass

Nice one!
I don't use __slots__ much at all, and if you'd said "attribute1" etc. I'd
have understood, but I'm really curious why would you be listing any
methods in __slots__?

Those should of course have been attributes - I noticed immediately
after posting ;-)

Aside: __slots__ is only really useful when you've created so many
objects you are running out of memory and you need to optimise memory
usage a bit. We got our app down to 1/3 of the memory usage by
putting in three __slots__
 
M

MonkeeSage

Nick said:
These are snatched straight from perl. In perl they are spelt
slightly differently

Yup, they are; perl had _some_ good ideas; no doubt. ;)
In perl (and maybe in ruby I don't know) the { } can be replaced with
any two identical chars, or the matching pair if bracketty, so q/blah/
or q(blah).

Yes; same thing in ruby.

Regards,
Jordan
 
J

James Stroud

hg said:
But today ? what is the cost of replacing %w("blah blah") by
Hi_I_Want_To_Split_The_String_That_Follows( "blah blah")

The latter is beginning to look like the Cocoa/NextStep framework.
Perhaps we should give up scripting languages for ObjC?

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top