Ruby on Rails Friday, February 22, 2019

You probably could use something like eval. That being said, if you refactor in order to avoid this, it should lead to clearer, safer code
--
Tales.


On Fri, Feb 22, 2019 at 9:36 AM Sapna Mishra <sapna.spaceo@gmail.com> wrote:
def conditions_combination step_conditions_labels
    conditions_combination_hash = Hash.new
    1.upto(step_conditions_labels.size).flat_map do |n|
      conditions_combination = ""
      step_conditions_labels.permutation(n).map { |condition|  
        if condition.length == n
          conditions_combination += "( "
          condition.each do |condition_label|
            conditions_combination += condition_label+" && "
          end
          conditions_combination = conditions_combination.chomp(' && ')
          conditions_combination += " ) || "
        end
        conditions_combination_hash[n.to_s] = conditions_combination.chomp(' || ').chomp(' && ')   
      } 
    end
    cond = false
    condition_check = conditions_combination_hash[step_conditions_labels.length.to_s]

    if condition_check
      cond = true
    end
    return cond
  end

I have created this function 

conditions_combination_hash = conditions_combination(['true','false','true'])

which gives me different combination like this 

{"1"=>"( true ) || ( false ) || ( true )", "2"=>"( true && false ) || ( true && true ) || ( false && true ) || ( false && true ) || ( true && true ) || ( true && false )", "3"=>"( true && false && true ) || ( true && true && false ) || ( false && true && true ) || ( false && true && true ) || ( true && true && false ) || ( true && false && true )"}

Issue is now I am not able use this combination to check whether it satisfies the condition as it is consider as string class.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/be2b5076-5a38-404a-a630-e7bfa9aeb30f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAJ_sHYNPGrkgqpBHG_crjB48DQcNjJ6iL8FSdZj7n7EpiSfOrQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment