View code regular expression

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

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

Francois GORET wrote:

<%= “checked” if @roles_users == 1 %>

Note that the ‘if’ must have a corresponding ‘end’.
Not so:

[email protected]:~$ 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…

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

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

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” :slight_smile:

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 ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

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

On Sat, 2006-04-15 at 18:36 -0400, [email protected] 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

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