[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A ruby-esque "split" that respects word groups.

Kenosis

9/14/2007 12:13:00 AM

Greetings friends,

I wondering if anyone here has some Ruby-fu they can share with WRT
for following problem. I've toyed with this today and don't like my
solution so I thought I'd run it by y'all. In summary, suppose we
have the following input, say to a command line interpreter:

foo bar "baz 42" "matz ruby yarv" jruby

Running split on this string, of course, yeilds:

['foo', 'bar', '"baz', '42"', '"matz', 'ruby', 'yarv"', 'jruby']

What I'd like instead is a "split" that respects the quoted word
groupings, which would result in the following instead:

['foo', 'bar', 'baz 42', 'matz ruby yarv', 'jruby']

Thanks in advance for any ideas you can lend!

Cheers,

Ken

3 Answers

Kenosis

9/14/2007 12:18:00 AM

0

On Sep 13, 5:12 pm, Kenosis <keno...@gmail.com> wrote:
> Greetings friends,
>
> I wondering if anyone here has some Ruby-fu they can share with WRT
> for following problem. I've toyed with this today and don't like my
> solution so I thought I'd run it by y'all. In summary, suppose we
> have the following input, say to a command line interpreter:
>
> foo bar "baz 42" "matz ruby yarv" jruby
>
> Running split on this string, of course, yeilds:
>
> ['foo', 'bar', '"baz', '42"', '"matz', 'ruby', 'yarv"', 'jruby']
>
> What I'd like instead is a "split" that respects the quoted word
> groupings, which would result in the following instead:
>
> ['foo', 'bar', 'baz 42', 'matz ruby yarv', 'jruby']
>
> Thanks in advance for any ideas you can lend!
>
> Cheers,
>
> Ken

My cleanest solution thus far, which does not properly handled nested
quotes:

a = "foo 'bar baz bim' 42 'java jruby jar'"
puts a.split("'").each { |b| b.strip! }

James Gray

9/14/2007 12:45:00 AM

0

On Sep 13, 2007, at 7:15 PM, Kenosis wrote:

> I wondering if anyone here has some Ruby-fu they can share with WRT
> for following problem. I've toyed with this today and don't like my
> solution so I thought I'd run it by y'all. In summary, suppose we
> have the following input, say to a command line interpreter:
>
> foo bar "baz 42" "matz ruby yarv" jruby
>
> Running split on this string, of course, yeilds:
>
> ['foo', 'bar', '"baz', '42"', '"matz', 'ruby', 'yarv"', 'jruby']
>
> What I'd like instead is a "split" that respects the quoted word
> groupings, which would result in the following instead:
>
> ['foo', 'bar', 'baz 42', 'matz ruby yarv', 'jruby']

>> require "shellwords"
=> true
>> Shellwords.shellwords(%q{foo bar "baz 42" "matz ruby yarv" jruby})
=> ["foo", "bar", "baz 42", "matz ruby yarv", "jruby"]

Hope that helps.

James Edward Gray II

Kenosis

9/14/2007 1:09:00 AM

0

On Sep 13, 5:45 pm, James Edward Gray II <ja...@grayproductions.net>
wrote:
> On Sep 13, 2007, at 7:15 PM, Kenosis wrote:
>
> > I wondering if anyone here has some Ruby-fu they can share with WRT
> > for following problem. I've toyed with this today and don't like my
> > solution so I thought I'd run it by y'all. In summary, suppose we
> > have the following input, say to a command line interpreter:
>
> > foo bar "baz 42" "matz ruby yarv" jruby
>
> > Running split on this string, of course, yeilds:
>
> > ['foo', 'bar', '"baz', '42"', '"matz', 'ruby', 'yarv"', 'jruby']
>
> > What I'd like instead is a "split" that respects the quoted word
> > groupings, which would result in the following instead:
>
> > ['foo', 'bar', 'baz 42', 'matz ruby yarv', 'jruby']
>
> >> require "shellwords"
> => true
> >> Shellwords.shellwords(%q{foo bar "baz 42" "matz ruby yarv" jruby})
> => ["foo", "bar", "baz 42", "matz ruby yarv", "jruby"]
>
> Hope that helps.
>
> James Edward Gray II

Yes, Jame - that helps a lot :)

Best,

Ken