Simple question(s)

(I think…)

Having a brain-fade today. Is there a simple ruby equivalent to
perl’s qw() construct? I want, in order to save typing, to convert a
list of non-quoted words like so:

foo bar baz…

into an array of quoted strings: [“foo”, “bar”, “baz”, …]

Perl does this by: @list = qw(foo bar baz …);

this works… list = “foo bar baz”.scan(/\w+/)
as does: list = “foo bar baz”.split(/ /)

but both seem wieldy and heavy-handed.

#2

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

Sorry for being so daft…had a long weekend.

···

Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

— Michael Campbell michael_s_campbell@yahoo.com wrote: > (I think…)

Having a brain-fade today. Is there a simple ruby equivalent to
perl’s qw() construct? I want, in order to save typing, to convert a
list of non-quoted words like so:

foo bar baz…

into an array of quoted strings: [“foo”, “bar”, “baz”, …]

Perl does this by: @list = qw(foo bar baz …);

this works… list = “foo bar baz”.scan(/\w+/)
as does: list = “foo bar baz”.split(/ /)

list = %q(foo bar baz)

but both seem wieldy and heavy-handed.

#2

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

this = “Dec 12”
that = $1 if this =~ /(\w+) (\d+)/
other = $2 if this =~ /(\w+) (\d+)/

HTH,

– Thomas Adam

···

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://uk.messenger.yahoo.com/

list = %W{foo bar baz}

···

On Tue, 2 Sep 2003 22:59:33 +0900, Michael Campbell michael_s_campbell@yahoo.com wrote:

into an array of quoted strings: [“foo”, “bar”, “baz”, …]

Perl does this by: @list = qw(foo bar baz …);


Dean saor, dean saor an spiorad. Is seinn d’orain beo.

Hi –

(I think…)

Having a brain-fade today. Is there a simple ruby equivalent to
perl’s qw() construct? I want, in order to save typing, to convert a
list of non-quoted words like so:

foo bar baz…

into an array of quoted strings: [“foo”, “bar”, “baz”, …]

%w{one two three} # => [“one”,“two”,“three”]

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

You mean you want Ruby to give you three systems for doing this? :slight_smile:

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

As of 1.8.0, one way would be:

require ‘scanf’
mon, day = “Dec 12”.scanf(“%s%d”)

(or “%s%s” if you want 12 as a string rather than an integer)

Or (also 1.8.0):

mon,day = /(\w+) (\d+)/.match(“Dec 12”).captures

Or you could do:

mon,day = *“Dec 12”.scan(/(\w+) (\d+)/)

(if you really have some reason to want to avoid dealing explicitly with
a MatchData object)

David

···

On Tue, 2 Sep 2003, Michael Campbell wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

(I think…)

Having a brain-fade today. Is there a simple ruby equivalent to
perl’s qw() construct? I want, in order to save typing, to convert a
list of non-quoted words like so:

foo bar baz…

into an array of quoted strings: [“foo”, “bar”, “baz”, …]

Perl does this by: @list = qw(foo bar baz …);

this?

a=%w( foo bar baz)
=> [“foo”, “bar”, “baz”]

···

il Tue, 2 Sep 2003 22:59:33 +0900, Michael Campbell michael_s_campbell@yahoo.com ha scritto::

That surely was meant to be

list = %w(foo bar baz)

– fxn

···

On Tuesday 02 September 2003 16:05, Thomas Adam wrote:

list = %q(foo bar baz)

str = “Dec 12”
regex = /(\w+) (\d+)/
mon, day = regex.match(str).captures
_, mon, day = regex.match(str).to_a

In one line:

mon, day = /(\w+) (\d+)/.match(“Dec 12”).captures

Gavin

···

On Wednesday, September 3, 2003, 12:05:05 AM, Thomas wrote:

#2

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

this = “Dec 12”
that = $1 if this =~ /(\w+) (\d+)/
other = $2 if this =~ /(\w+) (\d+)/

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

You mean you want Ruby to give you three systems for doing this?
:slight_smile:

220, 221, whatever it takes. =)

···

Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

MatchData has a #captures method as of 1.8,so you could also do
something like:

this = “Dec 12”
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

…but this will run into problems when there’s not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
that, other = m.captures

Do stuff with “that” and “other”

else

We didn’t match, do something about it

end

Also, if I was going to do that way you listed, I’d say it like:

if this =~ /(\w+) (\d+)
that, other = $1, $2
end

or even:

that, other = $1, $2 if this =~ /(\w+) (\d+)

but this still runs into the problem of when there’s no match.

Jason Creighton

···

On Tue, 2 Sep 2003 23:05:05 +0900 Thomas Adam thomas_adam16@yahoo.com wrote:

— Michael Campbell michael_s_campbell@yahoo.com wrote: > (I think…)

#2

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

this = “Dec 12”
that = $1 if this =~ /(\w+) (\d+)/
other = $2 if this =~ /(\w+) (\d+)/

— Xavier Noria fxn@hashref.com wrote: > On Tuesday 02 September 2003
16:05, Thomas Adam wrote:

list = %q(foo bar baz)

That surely was meant to be

list = %w(foo bar baz)

LOL, yes quite right – one of my freudian slips. Sorry about that.

– Thomas Adam

···

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://uk.messenger.yahoo.com/

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

this = “Dec 12”
that = $1 if this =~ /(\w+) (\d+)/
other = $2 if this =~ /(\w+) (\d+)/

str = “Dec 12”
regex = /(\w+) (\d+)/
mon, day = regex.match(str).captures
_, mon, day = regex.match(str).to_a

In one line:

mon, day = /(\w+) (\d+)/.match(“Dec 12”).captures

Ah hah, #captures was what I was searching for. Thanks Gavin (and
everyone else that replied.)

···

Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

— Jason Creighton androflux@softhome.net wrote:

— Michael Campbell michael_s_campbell@yahoo.com wrote: > (I
think…)

#2

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

this = “Dec 12”
that = $1 if this =~ /(\w+) (\d+)/
other = $2 if this =~ /(\w+) (\d+)/

MatchData has a #captures method as of 1.8,so you could also do
something like:

this = “Dec 12”
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

That’s right, Jason – I couldn’t for the life of me remember what it was
:slight_smile: Incidentally, small world, is it not :slight_smile:

…but this will run into problems when there’s not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
that, other = m.captures

Do stuff with “that” and “other”

else

We didn’t match, do something about it

end

I think we can make the assumption in this case that there will be a
match.

Also, if I was going to do that way you listed, I’d say it like:

if this =~ /(\w+) (\d+)
that, other = $1, $2
end

Each to their own.

– Thomas Adam

···

On Tue, 2 Sep 2003 23:05:05 +0900 > Thomas Adam thomas_adam16@yahoo.com wrote:

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk

— Jason Creighton androflux@softhome.net wrote:

— Michael Campbell michael_s_campbell@yahoo.com wrote: > (I
think…)

#2

Is there a way to get matched data into variables without using
matchdata, or perl ugly-variables?

Again, in perl I could do something like:

$_ = “Dec 12”;
(mon, day) = /(\w+) (\d+)/;

this = “Dec 12”
that = $1 if this =~ /(\w+) (\d+)/
other = $2 if this =~ /(\w+) (\d+)/

MatchData has a #captures method as of 1.8,so you could also do
something like:

this = “Dec 12”
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

That’s right, Jason – I couldn’t for the life of me remember what it was
:slight_smile: Incidentally, small world, is it not :slight_smile:

…but this will run into problems when there’s not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
that, other = m.captures

Do stuff with “that” and “other”

else

We didn’t match, do something about it

end

I think we can make the assumption in this case that there will be a
match.

Also, if I was going to do that way you listed, I’d say it like:

if this =~ /(\w+) (\d+)
that, other = $1, $2
end

Each to their own.

– Thomas Adam

···

On Tue, 2 Sep 2003 23:05:05 +0900 > Thomas Adam thomas_adam16@yahoo.com wrote:

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk

Good point. I’d probably use exception handling.

that, other = REGEX.match(STR).captures rescue

The two variables will receive nil if the match fails. That’s
slightly dodgy (may catch other exception?) but concise. If you want
to handle it more explicitly, then

begin
that, other = REGEX.match(STR).captures
rescue NoMethodError
# handle it
end

Gavin

···

On Thursday, September 4, 2003, 12:04:47 PM, Jason wrote:

this = “Dec 12”
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

…but this will run into problems when there’s not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
that, other = m.captures

Do stuff with “that” and “other”

else

We didn’t match, do something about it

end

Presumably, one can also use a “try…catch” scenario here too?

– Thomas Adam

— Gavin Sinclair gsinclair@soyabean.com.au wrote:

···

On Thursday, September 4, 2003, 12:04:47 PM, Jason wrote:

this = “Dec 12”
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

…but this will run into problems when there’s not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
that, other = m.captures

Do stuff with “that” and “other”

else

We didn’t match, do something about it

end

Good point. I’d probably use exception handling.

that, other = REGEX.match(STR).captures rescue

The two variables will receive nil if the match fails. That’s
slightly dodgy (may catch other exception?) but concise. If you want
to handle it more explicitly, then

begin
that, other = REGEX.match(STR).captures
rescue NoMethodError
# handle it
end

Gavin

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk

Presumably, one can also use a “try…catch” scenario here too?

“try” and “catch” mean nothing in Ruby.

Gavin

···

On Thursday, September 4, 2003, 10:55:27 PM, Thomas wrote:

— Gavin Sinclair gsinclair@soyabean.com.au wrote:

On Thursday, September 4, 2003, 12:04:47 PM, Jason wrote:

this = “Dec 12”
that, other = /(\w+) (\d+)/.match(this).captures
p that, other

…but this will run into problems when there’s not a match, and
Regexp#match returns nil. So something like this might be better

if m = /(\w+) (\d+)/.match(this)
that, other = m.captures

Do stuff with “that” and “other”

else

We didn’t match, do something about it

end

Good point. I’d probably use exception handling.

that, other = REGEX.match(STR).captures rescue

The two variables will receive nil if the match fails. That’s
slightly dodgy (may catch other exception?) but concise. If you want
to handle it more explicitly, then

begin
that, other = REGEX.match(STR).captures
rescue NoMethodError
# handle it
end

Gavin

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com