universal symbol

Discussion in 'Ruby' started by vahag vardanyan, Mar 11, 2011.

  1. HI

    I just can't explain in English what I want , so there is example

    in Unix the * symbol can replace the all sybmbols in string

    file111
    file112
    file113
    file114
    file115

    find file* #=>

    file111
    file112
    file113
    file114
    file115

    how can I do it in ruby
    my example is
    while(line=f.gets(sep="class#{i} here will be any text "))
    ...
    ...
    i+=1
    end

    --
    Posted via http://www.ruby-forum.com/.
    vahag vardanyan, Mar 11, 2011
    #1
    1. Advertising

  2. Hi,

    You are speaking about "shell globbing", you can use Dir.glob or its
    alias Dir[] like this:

    Dir.glob("*")
    Dir["*"]


    On Fri, Mar 11, 2011 at 11:36:18PM +0900, vahag vardanyan wrote:
    > HI
    >
    > I just can't explain in English what I want , so there is example
    >
    > in Unix the * symbol can replace the all sybmbols in string
    >
    > file111
    > file112
    > file113
    > file114
    > file115
    >
    > find file* #=>
    >
    > file111
    > file112
    > file113
    > file114
    > file115
    >
    > how can I do it in ruby
    > my example is
    > while(line=f.gets(sep="class#{i} here will be any text "))
    > ...
    > ...
    > i+=1
    > end
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    Markus Schirp, Mar 11, 2011
    #2
    1. Advertising

  3. thanks for answer, but I don't mean that, I don't mean directories

    actulay I mean, is there any sybol, like * in Unix!

    --
    Posted via http://www.ruby-forum.com/.
    vahag vardanyan, Mar 11, 2011
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    With a regexp you can do something similar:

    ['file111','file222','file1ax', 'something'].select { |str| str =~ /file.*/
    }
    #=> ['file111','file222','file1ax']

    So the unix * in ruby can be the regexp .* (the dot is important)

    2011/3/11 vahag vardanyan <>

    > thanks for answer, but I don't mean that, I don't mean directories
    >
    > actulay I mean, is there any sybol, like * in Unix!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Gunther Diemant, Mar 11, 2011
    #4
  5. vahag vardanyan

    Chris Hulan Guest

    On Mar 11, 10:10 am, vahag vardanyan <> wrote:
    > thanks for answer, but I don't mean that, I don't mean directories
    >
    > actulay I mean, is there any sybol, like * in Unix!
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    if the strings are all starting with the same chars, you could use
    String.start_with?()

    if its more complex you need Regular Expressions (Regexp)

    cheers
    Chris Hulan, Mar 11, 2011
    #5
  6. vahag vardanyan

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Fri, Mar 11, 2011 at 8:36 AM, vahag vardanyan
    <>wrote:

    >
    > how can I do it in ruby
    > my example is
    > while(line=f.gets(sep="class#{i} here will be any text "))
    > ...
    > ...
    > i+=1
    > end
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Sorry, what is this supposed to do? It looks like you are wanting to use
    glob patterns in your line delimiter? I suspect this is not the best
    approach to solving this problem, could you give an example of the input
    file, and what values you are wanting your variable "line" to take?
    Josh Cheek, Mar 11, 2011
    #6
  7. vahag vardanyan

    7stud -- Guest

    > my example is
    > while(line=f.gets(sep="class#{i} here will be any text "))
    > ...
    > ...
    > i+=1
    > end


    gets() does not let you specify wild cards like * (nor a regex), so the
    short answer is you can't do what you want.

    However, you could read the whole file into a variable, and then scan()
    the resulting string for chunks of text that match a regex, like:

    /class#\d .*? (?=class)/xms

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Mar 12, 2011
    #7
  8. vahag vardanyan

    7stud -- Guest

    str = "class#1 hello world class#2 goodbye class#3 hi"

    arr = str.split(/ (class\#\d) /xms)
    p arr

    arr.shift

    while arr.length > 0
    puts arr.slice!(0,2).join
    end

    --output:--
    ["", "class#1", " hello world ", "class#2", " goodbye ", "class#3", "
    hi"]
    class#1 hello world
    class#2 goodbye
    class#3 hi

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Mar 12, 2011
    #8
  9. vahag vardanyan

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Fri, Mar 11, 2011 at 9:12 PM, 7stud -- <> wrote:

    > str = "class#1 hello world class#2 goodbye class#3 hi"
    >
    > arr = str.split(/ (class\#\d) /xms)
    > p arr
    >
    > arr.shift
    >
    > while arr.length > 0
    > puts arr.slice!(0,2).join
    > end
    >
    > --output:--
    > ["", "class#1", " hello world ", "class#2", " goodbye ", "class#3", "
    > hi"]
    > class#1 hello world
    > class#2 goodbye
    > class#3 hi
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    It still needs to be better defined, his/her example delimiter is "class#{i}
    here will be any text ", so it isn't correct to assume the delimiter
    ends after the first digit. We really need more data, to understand what
    he/she actually wants to have happen, if you throw a splat in there, it will
    eat up the rest of the string.

    Another issue that could arise is if input is a large file, it could be too
    big to fit into memory, in this case, you might be able to use strscan from
    the standard library, or maybe have to find a gem to handle it, not sure, OP
    needs to give more information.
    Josh Cheek, Mar 12, 2011
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jens Mander
    Replies:
    0
    Views:
    494
    Jens Mander
    Jun 10, 2005
  2. Jens Mander
    Replies:
    2
    Views:
    1,359
    Jerry Coffin
    Sep 1, 2005
  3. baumann@pan
    Replies:
    1
    Views:
    732
    Richard Bos
    Apr 15, 2005
  4. Song Ma
    Replies:
    2
    Views:
    221
    Charles Oliver Nutter
    Jul 20, 2008
  5. Replies:
    6
    Views:
    1,737
Loading...

Share This Page