# View code regular expression

**URL:** <https://www.ruby-forum.com/t/view-code-regular-expression/93943>\
**Category:** Rails\
**Created:** [April 15, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943 "2006-04-15T17:37:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/1 "2006-04-15T17:37:55Z")

</div>

I’m lost on regular expressions to begin with…

I’m trying to fix a value to one of 4 radio buttons as there will be  
value of either 1,2,3 or 4 in @roles\_users…

 /\> /\> /\> /\>

but this code gets me a syntax error…

@roles\_users is a hash created from a find.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/2 "2006-04-15T17:59:24Z")

</div>

On Sat, 2006-04-15 at 22:47 +0700, Francois GORET wrote:

> > ```
> > value="Case Manager Admin"
> > 
> > ```
> 
> > ```
> > name="case_managers[case_manager_name]"
> > value="Case Manager None"
> > <% if =~ @roles_users /4/ checked = "checked" %> /></TD>
> > 
> > ```
> > 
> > but this code gets me a syntax error…
> > 
> > @roles\_users is a hash created from a find.
> 
> Hmm… so it’s not an integer / string like I understood from the beginning?  
> What’s the contents of your hash?

* * *

@roles\_users.inspect says…  
[#“3”, “user\_id”=\>“18”}\>, #“7”, “user\_id”=\>“18”}\>, #“11”,  
“user\_id”=\>“18”}\>, #“15”, “user\_id”=\>“18”}\>, #“19”, “user\_id”=\>“18”}\>]

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/3 "2006-04-15T18:24:12Z")

</div>

Francois GORET wrote:

> \<%= “checked” if @roles\_users == 1 %\>
> 
> Note that the ‘if’ must have a corresponding ‘end’.  
> Not so:

alex@pandora:~$ irb  
irb(main):001:0\> puts ‘foo’ if 1 == 1  
foo  
=\> nil  
irb(main):002:0\> puts ‘foo’ if 1 == 0  
=\> nil  
irb(main):003:0\>

I don’t think I’ve ever tried that syntax in ERb, though, so I can’t  
vouch for it in a template…

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 15, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/4 "2006-04-15T17:50:13Z")

</div>

On Saturday 15 April 2006 22:34, Craig W. wrote:

> I’m lost on regular expressions to begin with…
> 
> I’m trying to fix a value to one of 4 radio buttons as there will be  
> value of either 1,2,3 or 4 in @roles\_users…

So @roles\_users is an integer, or a string ? I assume an integer…

> /\>

\<%= “checked” if @roles\_users == 1 %\>

Note that the ‘if’ must have a corresponding ‘end’. The one line form of  
‘if’  
has a mandatory ‘then’ too:  
if @roles\_users == 1 then “checked” end … is the verbose equivalent of  
the  
expression above.

The syntax for a regular expression is @roles\_users =~ /1/

> ```
> value="Case Manager None"
> <% if =~ @roles_users /4/ checked = "checked" %> /></TD>
> 
> ```
> 
> but this code gets me a syntax error…
> 
> @roles\_users is a hash created from a find.

Hmm… so it’s not an integer / string like I understood from the  
beginning?  
What’s the contents of your hash?

Francois

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 16, 2006, 12:05am UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/5 "2006-04-16T00:05:31Z")

</div>

On Sat, 2006-04-15 at 17:21 +0100, Alex Y. wrote:

> irb(main):002:0\> puts ‘foo’ if 1 == 0  
> =\> nil  
> irb(main):003:0\>
> 
> I don’t think I’ve ever tried that syntax in ERb, though, so I can’t  
> vouch for it in a template…

* * *

still I’m struggling with this…in script/console

> > @roles\_users  
> > =\> [#\<RolesUsers:0xb796f490 @attributes={“role\_id”=\>“3”,  
> > “user\_id”=\>“18”}\>, #\<RolesUsers:0xb796f454 @attributes={“role\_id”=\>“7”,  
> > “user\_id”=\>“18”}\>, #\<RolesUsers:0xb796f404 @attributes={“role\_id”=\>“11”,  
> > “user\_id”=\>“18”}\>, #\<RolesUsers:0xb796f2c4 @attributes={“role\_id”=\>“15”,  
> > “user\_id”=\>“18”}\>, #\<RolesUsers:0xb796f274 @attributes={“role\_id”=\>“19”,  
> > “user\_id”=\>“18”}\>]

I’m trying to figure out if there’s a ‘role\_id’ that equals 1, 2, 3 or 4  
from the above…

> > 3 =~ /@roles\_users/  
> > =\> false
> 
> > “3” =~ /@roles\_users/  
> > =\> nil
> 
> > ‘:role\_id=\>“3”’ =~ /@roles\_users/  
> > =\> nil
> 
> > ‘role\_id=\>“3”’ =~ /@roles\_users/  
> > =\> nil
> 
> > ‘role\_id=\>3’ =~ /@roles\_users/  
> > =\> nil

If I assume which element, I can get it but I can’t just assume that  
(well I can if I ‘order’ the items when I retrieve them) but I sure  
would love to know how to use regex to parse @roles\_users anyway.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 16, 2006, 12:39am UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/6 "2006-04-16T00:39:14Z")

</div>

Hi –

On Sat, 15 Apr 2006, Craig W. wrote:

> > =\> nil  
> > =\> [#\<RolesUsers:0xb796f490 @attributes={“role\_id”=\>“3”,  
> > =\> false
> > 
> > > “3” =~ /@roles\_users/  
> > > =\> nil  
> > > ‘:role\_id=\>“3”’ =~ /@roles\_users/  
> > > =\> nil  
> > > ‘role\_id=\>“3”’ =~ /@roles\_users/  
> > > =\> nil  
> > > ‘role\_id=\>3’ =~ /@roles\_users/  
> > > =\> nil

You’re performing various tests on the string “@roles\_users”, which  
you can tell at a glance doesn’t contain a “3” 🙂

> If I assume which element, I can get it but I can’t just assume that  
> (well I can if I ‘order’ the items when I retrieve them) but I sure  
> would love to know how to use regex to parse @roles\_users anyway.

@roles\_users is an array, and regexes are for strings, so it’s not the  
right way. Here’s a way to see whether the array contains an element  
whose role\_id is “1” through “4”:

@roles\_users.any? {|ru| %w{1 2 3 4}.include?(ru.role\_id) }

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [April 17, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/view-code-regular-expression/93943/7 "2006-04-17T17:17:29Z")

</div>

On Sat, 2006-04-15 at 18:36 -0400, removed\_email\_address@domain.invalid wrote:

> > > 
> > 
> > * * *
> > 
> > I’m trying to figure out if there’s a ‘role\_id’ that equals 1, 2, 3 or 4
> > 
> > > > ‘role\_id=\>3’ =~ /@roles\_users/  
> > > > right way. Here’s a way to see whether the array contains an element  
> > > > whose role\_id is “1” through “4”:
> 
> @roles\_users.any? {|ru| %w{1 2 3 4}.include?(ru.role\_id) }

* * *

thanks - that was exactly what I need as I am likely to re-factor my  
code because I really don’t like ‘assuming’ the data isn’t going to  
change, even though I am in control over the data in this case. I  
recognize my propensity for shooting myself in the foot and this section  
of code bothered me and what you pointed out appears to be a much better  
solution, but I had to move on at the time and thus, this information  
will wait a few days as I now have things working and will do some  
cleanup over time.

Thanks

Craig
