How do I format the "Diff" output in cucumber - ruby-on-rails

I have a scenario outline that compares the results of a method to the array that should be returned. So I get a series of statements like this when they don't match:
expected: ["a", "b", "c", "d", "e", "f"]
got: ["c", "d", "e", "f", "g"] (using ==)
Diff:
## -1,2 +1,8 ##
-["a", "b", "c", "d", "e", "f"]
+["c",
+ "d",
+ "e",
+ "f",
+ "g"]
This is not the most succinct or helpful output. It would be much more helpful to be dipslay like:
expected: ["a", "b", "c", "d", "e", "f"]
got: ["c", "d", "e", "f", "g"] (using ==)
Diff:
## -1,2 +1,8 ##
-["a", "b"]
+["g"]
This way I could instantly see what values were additional or missing.

Use the Array difference method:
(expected_array - actual_array).should == []

Related

How to iterate through array of arrays on Rails template?

I've got an array in the following form:
[["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
Is there a way to somehow display this information on a Rails template using iterators? I need something like this:
First - a, b, c
Second - d, e,
Third - g, h, i.
Or this is impossible and I should modify the initial array form?
Thanks in advance.
Without modifing the main array you could try with each_with_index inside each for the main array of arrays, then checking for the first value you can skip it and get the array of letters:
array = [["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
array.each do |main|
main.each_with_index do |value, index|
next if index.zero?
p value
end
end
# => ["a", "b", "c"]
# ["d", "e"]
#  ["g", "h", "i"]
Or if you want to access it as a hash it'd be easier:
array = [["First", ["a", "b", "c"]], ["Second", ["d", "e"]], ["Third", ["g", "h", "i"]]]
array.to_h.each do |_, value|
p value
end
# => ["a", "b", "c"]
# ["d", "e"]
#  ["g", "h", "i"]

How can I generate array combinations such that so two elements in the array can not be together in any of combinations

Suppose, I have an array of five elements ['a','b','c','d','e']. I want combinations of four elements but I want that 'a' and 'b' not be together in any of combinations. How would I do that?
Expected result should be
['a', 'c', 'd', 'e']
['b', 'c', 'd', 'e']
Please write a more generalize solution.I am using ruby's combination method.I here just write an example.It may be possible that the combination number may vary(like there may be a array of 9 with size of 7 combinations) and also It may needed that I want two elements say a and e and d and f should not be together in any of combination.I know it bit confusing please let me know if I need to explain.Would really appreciate any help.
['a','b','c','d','e'].permutation(4).reject do |e|
e.include?('a') && e.include?('b')
end
Or, if you do not care about element order (credits to #mark-thomas,) use Array#combination:
['a','b','c','d','e'].combination(4).reject do |e|
e.include?('a') && e.include?('b')
end
#⇒ [["a", "c", "d", "e"], ["b", "c", "d", "e"]]
Please note, this approach is eager to resources. I’dn’t recommend to use it on big arrays.
I think you are looking for something like that:
def combine(array, should_not_be_together, size)
aux = []
add_together = true
while aux.size < size
random = Random.rand(2)
if random == 1
aux << array.sample
elsif add_together
aux << should_not_be_together.sample
add_together = false
end
aux.uniq!
end
aux
end
array = ['c', 'd', 'e']
should_not_be_together = ['a', 'b']
size = 4
combine(array, should_not_be_together, size)
Results:
# => ["c", "a", "d", "e"]
# => ["e", "b", "c", "d"]
# => ["e", "a", "c", "d"]
# => ["c", "d", "b", "e"]
# => ["d", "b", "e", "c"]
# => ["a", "c", "e", "d"]
# => ["c", "b", "d", "e"]
# => ["c", "a", "e", "d"]

Split an array in many parts [duplicate]

This question already has answers here:
Options for processing X elements of an array at a time using Ruby
(2 answers)
Closed 8 years ago.
In a Rails 3.2 application, I have an array like that :
arr = ["a", "b", "c", "d", "e"]
And I want to split it in 3 parts like this :
[["a", "b"], ["c", "d"], ["e"]]
I want to a have function to have the first part of this array.
I always want to have 3 parts. So, if I have :
["a", "b", "c", "d", "e", "f", "g", "h"]
I want to have :
[["a", "b", "c"], ["d", "e", "f"], ["g", "h"]]
Do you have a simple solution?
["a", "b", "c", "d", "e"].in_groups_of(2, false)
# => [["a", "b"], ["c", "d"], ["e"]]
If you always want 3 parts, this should do it
parts_count = 3
group_count = arr.length.fdiv(parts_count).ceil
arr.in_groups_of(group_count, false)
# => [["a", "b"], ["c", "d"], ["e"]]
More info here.

Separate single characters from an string using .split

I want to split a string of text into single chars excluding any kind of symbol and numbers and only allowing space with rails using split but I cannot reproduce the desired result.
Like
only_words_and_spaces = /[^a-zA-Z\s]/
"Hello friends".split(only_words_and_spaces)
=> ["H", "e", "l", "l", "o", " ", "f", "r", "i", "e", "n", "d", "s"]
The regex seems to work well, but I cannot find a way to split that string into a single char array.
You can split the string into array liek this:
"Hello friends".split(//)
You can try with
"Hello friends".split('')
there are options like
"Hello friends".scan(/./)
or
word = "Hello friends"
word.each_byte{|b| puts b.chr}
only_words_and_spaces = /[^a-zA-Z\s]/
"Hello friends+-!".gsub(only_words_and_spaces, '').split('')
=> ["H", "e", "l", "l", "o", " ", "f", "r", "i", "e", "n", "d", "s"]
The following does the trick:
"Hello 32☃ friends".scan(/[a-zA-Z\s]/)
#=> ["H", "e", "l", "l", "o", " ", " ", "f", "r", "i", "e", "n", "d", "s"]
The original string contains numbers and the UTF-8 snowman, but they are not present in the character array.

JSON Array to Hash

I need to parse and display solr facets which are returned in either JSON or Ruby formar:
Collections: [ "a", 1, "b", 2, "c", 3, "d", 4, ... ]
into
{"a"=>1, "b"=>2, "c"=>3, "d"=>4}
What is the cleanest way?
EDIT: Well now that we know what you actually want, a hash ...
collections = ["a", 1, "b", 2, "c", 3, "d", 4]
Hash[*collections]
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
Original answer: I may not understand your goal but...
collections = ["a", 1, "b", 2, "c", 3, "d", 4]
collections.each_slice(2).map{ |(x, y)| "#{x} - #{y}" }
# => ["a - 1", "b - 2", "c - 3", "d - 4"]
What i see you want to do is maybe a hash ? {a => "1", b => "2"} ??
If so, read below:
collections = [ "a", 1, "b", 2, "c", 3, "d", 4]
result = Hash[*collections.flatten]
result prints {"a"=>1, "b"=>2, "c"=>3, "d"=>4}

Resources