Ruby on Rails
Sunday, February 24, 2019
Hello Marco,
Thank you for replying to the query with detailed explanation. This is what I wanted to do in my code but the code was not clear but you tried to understand my issue and provided the solution so thanks for that. I was not aware with the combination method before.
On Saturday, February 23, 2019 at 2:40:53 AM UTC+5:30, Marco Antonio Almeida wrote:
-- Thank you for replying to the query with detailed explanation. This is what I wanted to do in my code but the code was not clear but you tried to understand my issue and provided the solution so thanks for that. I was not aware with the combination method before.
On Saturday, February 23, 2019 at 2:40:53 AM UTC+5:30, Marco Antonio Almeida wrote:
Hi Sapna,This is a fun exercise to do and also to do some refactoring after I got to understand what you are aiming to do.First of all, your code is not optimal yet. Let's look at the output of the example you gave:{"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 )"}The combination of 2 and 3 have duplicated information. For example: (true && false && true) are the same as (true && true && false). I have provided a solution for what I understood from your problem below with some comments.I am considering that you need to solve this in one method, otherwise, I would split in many different methods to make the code more clear. I am also being very cheeky here to introduce many features of the language that would make your life easier and your code much more understandable.I hope this helps.# Evaluate the combinations of conditions.## step_conditions_labels - The Array of conditions.## Examples## conditions_combination(%w[true false true])# # => {1=>true, 2=>true, 3=>false}## conditions_combination(%w[true false true false true])# # => {1=>true, 2=>true, 3=>true, 4=>false, 5=>false}## Returns a hash of the evaluation of respective combinations.def conditions_combination(step_conditions_labels) # Reduce the Array to the result Hash.step_conditions_labels.each_with_index.reduce({}) do |hash, (_, index)| # Update each of the Hash keys with the evalutation of the combination.hash.update(index + 1 =># Eval is the way to go here as Tales mentioned, but it's a very# dangerous method. Be careful!eval(# Array#combination is what does the trick for you. Check it out:# The other lines just help on building the condition String.step_conditions_labels.combination(index + 1).map { |combination| "(#{combination.join(' && ')})" }.join(' || ')))endendconditions_combination(%w[true false true])Best regards,/ MarcoOn Fri, Feb 22, 2019 at 6:56 PM Hassan Schroeder <hassan.s...@gmail.com> wrote:On Fri, Feb 22, 2019 at 4:36 AM Sapna Mishra <sapna....@gmail.com> wrote:
> Issue is now I am not able use this combination to check whether it satisfies the condition as it is consider as string class.
If I saw something like that in a PR I would reject it in ~2 seconds.
What actual problem are you trying to solve?
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote
--
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-ta...@googlegroups.com .
To post to this group, send email to rubyonra...@googlegroups.com .
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ .CACmC4yCg%2Bm9gv5% 3DuESBmdzYOSYCV% 3DCspNdbkhB3hucVEXgbFcQ% 40mail.gmail.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/a6777a65-fb5d-4f65-8b85-c208d4a6ec55%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment