# Does ruby support global expression?

**URL:** <https://www.ruby-forum.com/t/does-ruby-support-global-expression/157515>\
**Category:** Ruby\
**Created:** [January 12, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/does-ruby-support-global-expression/157515 "2009-01-12T15:00:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jarod\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Jarod\_Z](https://www.ruby-forum.com/u/Jarod_Z)\
**Post date:** [January 12, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/does-ruby-support-global-expression/157515/1 "2009-01-12T15:00:55Z")

</div>

Hi, all

```
 In my project, there are some rules using global expression( '*'

```

as one or more characters ) to match some functions’ output.  
Such as:  
rule1e$B!'e(B the \* apple (will match the output “the red  
apple”)  
rule2e$B!'e(B the \* + \* game (will match the output “the  
wonderful +  
successful game”)

```
 I want to use ruby to realize the match check, but I only know

```

ruby supports regular expression.  
So I have to change rules to the form of regular expressions( “\*” =\> “.  
+” and add ‘’ to other regular expession keywords like ‘+’, ‘.’ etc.)

```
 Is there any way to use global expression directly in ruby?

```

Thanks

---

<div class="post-metadata">

**Author:** ![Jarod\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Jarod\_Z](https://www.ruby-forum.com/u/Jarod_Z)\
**Post date:** [January 12, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/does-ruby-support-global-expression/157515/2 "2009-01-12T15:34:33Z")

</div>

On Mon, Jan 12, 2009 at 2:59 PM, Jarod Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ruby supports regular expression.  
> So I have to change rules to the form of regular expressions( “\*” =\> “.  
> +” and add '' to other regular expession keywords like ‘+’, ‘.’ etc.)
> 
> ```
> Is there any way to use global expression directly in ruby?
> 
> ```

No, better make it ourselves

class String  
def escape  
return “” if empty?  
Regexp::escape self  
end  
def glob? a\_string  
Regexp::new(  
scan( /(._?)\*([^_]\*)/ ).  
inject(“”) { | r , ( prefix, suffix ) | r \<\< prefix.escape \<\<  
‘\w+’ \<\< suffix.escape }  
).match( a\_string )  
end  
end

p “ab _c_d\*”.glob?( “ab andcordequally” )  
p “a\*b”.glob?( “ab” )  
p “etc.etc.”

HTH  
R.

---

<div class="post-metadata">

**Author:** ![Jarod\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Jarod\_Z](https://www.ruby-forum.com/u/Jarod_Z)\
**Post date:** [January 12, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/does-ruby-support-global-expression/157515/3 "2009-01-12T20:40:36Z")

</div>

On 12.01.2009 15:33, Robert D. wrote:

> > ```
> > I want to use ruby to realize the match check, but I only know
> > 
> > ```
> 
> ```
> Regexp::escape self
> 
> ```
> 
> p “ab _c_d\*”.glob?( “ab andcordequally” )  
> p “a\*b”.glob?( “ab” )  
> p “etc.etc.”

Small remark: I’d split this up, i.e.

class String  
def to\_glob  
Regexp::new(  
scan( /(._?)\*([^_]\*)/ ).  
inject("") { | r , ( prefix, suffix ) |  
r \<\< prefix.escape \<\< ‘\w+’ \<\< suffix.escape }  
)  
end  
end

This makes for more efficient matching if the pattern is to be applied  
to multiple strings.

Here’s how I’d do it:

class String  
def to\_glob  
Regexp.new(’\A’ \<\< gsub(/\([^_?])/, ‘\1’).split(/(\?[_?])/).map  
do |s|  
case s  
when ‘_’: '._’  
when ‘?’: ‘.’  
when ‘\*’, ‘\?’: s  
else Regexp.escape(s)  
end  
end.join \<\< ‘\z’)  
end  
end

gl = “foo\*b\ar\*a”.to\_glob  
p gl

%w{foo foobar_a foo123bar foobar_a}.each do |s|  
p [s, gl =~ s]  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Jarod\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Jarod\_Z](https://www.ruby-forum.com/u/Jarod_Z)\
**Post date:** [January 12, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/does-ruby-support-global-expression/157515/4 "2009-01-12T21:01:12Z")

</div>

On Mon, Jan 12, 2009 at 8:39 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Small remark: I’d split this up, i.e.  
> Spoiling all the fun for the profilers 😉  
> Good point though.  
> Robert
