universal symbol

V

vahag vardanyan

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
 
M

Markus Schirp

Hi,

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

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

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!
 
G

Gunther Diemant

[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)
 
C

Chris Hulan

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

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

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
 
J

Josh Cheek

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

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
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?
 
7

7stud --

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
 
7

7stud --

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
 
J

Josh Cheek

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

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
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top