Topic: [Solved]more ugly if statements

field    --  x amount of fields, condition     --  y amount of conditions, text     --  z amount of texts

I want to have all combinations possible, x amounts of fields match or mismatch with each of the y amount conditioons and set a defined text of z amount of text lines.

How can I refactor the code to avoid hard coding every possible combination ? Schematic example:

if
a[] == b[]
do c[]
end
if
a[] != b[]
do c[]
end

or, more defined:

def output
if field == condition
concat text  block.binding
end
if field != condition
concat text  block.binding
end
if field1 == condition
concat text  block.binding
end
if field1 != condition
concat text  block.binding
end
if field1 == condition1
concat text  block.binding
end
if field1 != condition1
concat text  block.binding
....
end

Last edited by ediestel (2007-12-18 02:19:10)

Re: [Solved]more ugly if statements

You probably want to do something involving calling Procs, but I'm not sure exactly what you're trying to achieve. Can you be more specific?

Re: [Solved]more ugly if statements

Have a look at ruby case statement

Re: [Solved]more ugly if statements

He said without hard-coding each possibility...the case statement makes it shorter, but doesn't eliminate the hardcoding.

Re: [Solved]more ugly if statements

Have you ever used the hash strategy pattern before? It's a common pattern in Python because there is no 'case' statement.

As in your example:

   
def output
  if field == condition
    concat text  block.binding
  end
  if field != condition
    concat text  block.binding
  end
  if field1 == condition
    concat text  block.binding
  end
  if field1 != condition
    concat text  block.binding
  end
  if field1 == condition1
    concat text  block.binding
  end
  if field1 != condition1
    concat text  block.binding
   ....
end

You'd probably want:

strategies = {
  :field => [{ field == condition }, { field != condition }],
  :field1 => [{ field1 == condition }, { field1 != condition }],
  :field2 => [{ field2 == condition }, { field2 != condition }],
  ...
}

Of course, you'd need to create the procs dynamically, which would require looking up the local variables dynamically.. You might have to stuff them inside an object first.

Last edited by farleyknight (2007-12-16 19:48:13)

Re: [Solved]more ugly if statements

I solved my problem by comparing arrays:  a = b | conditions ;

 
b = Array.new           # new array for blockfields
a = Array.new                        # array, all values
condiitons                              # array cotaining defaults

               blockfields.each_with_index do |blockfield, index|
                 blockfield = blockfield.to_s
b << blockfield                                      # array b
a = b | conditions                                   # array a           
    if  conditions != a                               # if some values are not 'default, a != conditions
         ...
                     
  end
      if conditions == a                            # if all values are 'default', a == conditions
          ...


The pupose: I am rendering lines of text only if the variables in those lines have non-default values. If the variables have default values, the text line is not shown. The text is captured as a block and parsed for variables. Array b contains all the variables in the text block, array conditions contains hard coded defaults.
If a == conditions (equal to b | conditions == conditions) => in that case it must be that b does not contain non default variables; if a != conditions, array b must contain non default variables.

In simple terms the helper method does: are all available variables equal or not equal to the hard coded variables ?

Last edited by ediestel (2007-12-18 02:15:26)