Universal symbol

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

Hi,

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

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

thanks for answer, but I don’t mean that, I don’t mean directories

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

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 [email protected]

On Mar 11, 10:10am, vahag vardanyan [email protected] 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

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. regexes
allow all sorts of wild cards.

To come up with a regex that will read the file into the chunks you
want, post a portion of the file you want to read.

Or, split() may even be able to do what you want.

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

Because your English is not good, it would be simplest if you posted:

  1. what a portion of your input file looks like, and
  2. what you want the output to be.

On Fri, Mar 11, 2011 at 8:36 AM, vahag vardanyan
[email protected]wrote:

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?

On Fri, Mar 11, 2011 at 9:12 PM, 7stud – [email protected]
wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs