#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    196
    Rep Power
    96

    Checking if a variable exists?


    I've got this line of type that outputs the quantity of a print job ordered. Some products can have multiple sets and some can not.

    For the products that CAN have multiple sets this works:

    Code:
    <%= web_set.quantity %><% if web_set.sets > 1 %> of <%= web_set.sets %><% end %>
    It will output "1000 of 10" or something along those lines.

    But, for the products that CAN NOT have multiple sets it breaks because web_set.sets doesn't exist.

    If I were writing this in PHP I would say:
    Code:
    if (isset($sets) AND $sets > 1) {...}
    But I'm not sure how to do this in Ruby. I've found some examples but because I don't know Ruby I'm not sure how to piece it together.

    Code:
    if defined?(web_set.sets && web_set.sets > 1)
      puts aaa # the error comes from this part, since aaa is not defined.
    end
    Code:
    <%= web_set.quantity %><% if defined? (web_set.sets && web_set.sets > 1 %> of <%= web_set.sets %><% end %>
    The first is the sample I found substituting my variables. The second is my guess but since I don't know much about Ruby and I don't have a production environment I'm hoping someone can confirm this will work as expected before I upload it and it breaks

    Thanks!

    Mike
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    196
    Rep Power
    96
    Tried a couple different things to no avail...

    Code:
    <% if defined? (web_set.sets && web_set.sets > 1 %> of <%= web_set.sets %><% end %>
    Code:
    <% if defined? (web_set.sets) && if (web_set.sets > 1) %> of <%= web_set.sets %><% end %>
    Code:
    <% if defined?web_set.sets %><% if web_set.sets > 1 %> of <%= web_set.sets %><% end %><% end %>

IMN logo majestic logo threadwatch logo seochat tools logo