Professional Web Applications Themes

Simple question(s) - Ruby

--- Michael Campbell <michael_s_campbell> 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 ...

  1. #1

    Default Re: Simple question(s)

    --- Michael Campbell <michael_s_campbell> 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" -- [url]www.linuxgazette.com[/url]

    __________________________________________________ ______________________
    Want to chat instantly with your online friends? Get the FREE Yahoo!
    Messenger [url]http://uk.messenger./[/url]

    Thomas Adam Guest

  2. #2

    Default Re: Simple question(s)

    On Tue, 2 Sep 2003 22:59:33 +0900, Michael Campbell
    <michael_s_campbell> wrote:

    ||| into an array of quoted strings: ["foo", "bar", "baz", ...]
    |||
    ||| Perl does this by: list = qw(foo bar baz ...);

    list = %W{foo bar baz}


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

    [url]http://www.joeygibson.com[/url]
    [url]http://www.joeygibson.com/blog/life/Wisdom.html[/url]


    Joey Gibson Guest

  3. #3

    Default Re: Simple question(s)

    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)

    -- fxn


    Xavier Noria Guest

  4. #4

    Default Re: Simple question(s)

    Hi --

    On Tue, 2 Sep 2003, Michael Campbell 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", ...]
    %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? :-)
    > 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

    --
    David Alan Black
    home: [email]dblacksuperlink.net[/email]
    work: [email]blackdavshu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  5. #5

    Default Re: Simple question(s)

    --- Xavier Noria <fxnhashref.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" -- [url]www.linuxgazette.com[/url]

    __________________________________________________ ______________________
    Want to chat instantly with your online friends? Get the FREE Yahoo!
    Messenger [url]http://uk.messenger./[/url]

    Thomas Adam Guest

  6. #6

    Default Re: Simple question(s)

    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+)/

    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


    Gavin Sinclair Guest

  7. #7

    Default Re: Simple question(s)

    > > 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?
    > :-)
    220, 221, whatever it takes. =)






    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free, easy-to-use web site design software
    [url]http://sitebuilder.[/url]

    Michael Campbell Guest

  8. #8

    Default Re: Simple question(s)

    On Tue, 2 Sep 2003 23:05:05 +0900
    Thomas Adam <thomas_adam16> wrote:
    > --- Michael Campbell <michael_s_campbell> 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

    ....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
    Jason Creighton Guest

  9. #9

    Default Re: Simple question(s)

    --- Jason Creighton <androfluxsofthome.net> wrote:
    > On Tue, 2 Sep 2003 23:05:05 +0900
    > Thomas Adam <thomas_adam16> wrote:
    >
    > > --- Michael Campbell <michael_s_campbell> 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
    :) Incidentally, small world, is it not :)
    > ....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

    =====
    Thomas Adam

    "The Linux Weekend Mechanic" -- [url]www.linuxgazette.com[/url]

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

    Thomas Adam Guest

  10. #10

    Default Re: Simple question(s)

    --- Jason Creighton <androfluxsofthome.net> wrote:
    > On Tue, 2 Sep 2003 23:05:05 +0900
    > Thomas Adam <thomas_adam16> wrote:
    >
    > > --- Michael Campbell <michael_s_campbell> 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
    :) Incidentally, small world, is it not :)
    > ....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

    =====
    Thomas Adam

    "The Linux Weekend Mechanic" -- [url]www.linuxgazette.com[/url]

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

    Thomas Adam Guest

  11. #11

    Default Re: Simple question(s)

    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


    Gavin Sinclair Guest

  12. #12

    Default Re: Simple question(s)

    Presumably, one can also use a "try..catch" scenario here too?

    -- Thomas Adam

    --- Gavin Sinclair <gsinclairsoyabean.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" -- [url]www.linuxgazette.com[/url]

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

    Thomas Adam Guest

  13. #13

    Default Re: Simple question(s)

    On Thursday, September 4, 2003, 10:55:27 PM, Thomas wrote:
    > Presumably, one can also use a "try..catch" scenario here too?
    "try" and "catch" mean nothing in Ruby.

    Gavin

    > --- Gavin Sinclair <gsinclairsoyabean.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" -- [url]www.linuxgazette.com[/url]

    Gavin Sinclair Guest

Similar Threads

  1. Need Simple Answer to Simple Contribute/Firefox question
    By Adobe nickname in forum Macromedia Contribute General Discussion
    Replies: 2
    Last Post: February 13th, 06:59 PM
  2. Very simple question, but not for me!!!
    By ross in forum Macromedia Flash
    Replies: 3
    Last Post: January 19th, 03:17 AM
  3. simple question but want help.....
    By chris in forum ASP Database
    Replies: 2
    Last Post: December 11th, 03:58 PM
  4. Simple CGI Question
    By Jack in forum PERL Beginners
    Replies: 7
    Last Post: November 7th, 08:06 AM
  5. Really simple Question.
    By Graham Mattingley in forum ASP
    Replies: 2
    Last Post: September 4th, 09:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139