Separate single characters from an string using .split - ruby-on-rails

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.

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.

How do I format the "Diff" output in cucumber

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 == []

how to cycle with whole arrays ruby on rails

I'm still new to ruby on rails and web development so please bear with me:
I have two arrays
a1 = [1,2,3,4]
b1 = [7,6,5,4]
I want to alternate which array i'm using; switching between a1[] and b1[].
I'm currently trying to use the cycle() command to accomplish this.
<% #good_bad = [7,6,5,4,3,2,1] %>
<% #bad_good = [1,2,3,4,5,6,7] %>
WITHOUT CYCLE:</br>
<% #super = #bad_good%>
<%= #super%>
<%= #super[0]%>
<%= #super[1]%>
<%= #super[2]%>
WITH CYCLE: </br>
<% #temp_array = cycle(#bad_good , #good_bad , :name => "rating")%>
<%= #temp_array%>
<%= #temp_array[0]%>
<%= #temp_array[1]%>
<%= #temp_array[2]%>
This will display:
ITHOUT CYCLE: 1234567 1 2 3 WITH CYCLE: 1234567 49 50 51
I would expect the print out to be the same since the first cycle it is storing the #temp to #bad_good.
There is probably something basic i'm missing. It's weird how when i try to get the single values of the array it print out 49,50,51, but when i print out the whole array it is accurate?
Any advice appreciated,
Thanks,
D
I believe the way cycle() works, it will convert the output to a string. So when you call #tempt_array[0], it returns something like "1234567"[0]
My tests in the console returned what I expected:
"1234567".to_s[0]
=> 49
"1234567".to_s[1]
=> 50
"1234567".to_s[2]
=> 51
I believe these are the ASCII character codes for "1", "2", and "3" as seen here: http://www.asciitable.com/
You'll probably need to write your own enumerator method, or find a different one to use.
I will admit that I'm not even sure how your code is working now, because cycle is an Enumerable method and you don't appear to be calling it on any enumerable object.
At any rate, to create a Enumerator that will cycle between two arrays forever, you'd do it like this:
(array1 + array2).cycle
So for example:
array1 = 1.upto(7).to_a
array2 = 7.downto(1).to_a
sequence = (array1 + array2).cycle
sequence.take 49
# => [1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7]
Edit: Karl's explanation provided the missing piece. I was thinking of Enumerable#cycle, but this is TextHelpers#cycle. I think the Enumerable method is actually closer to what you were looking for, though.
You could also cut out the cycle completely and just use
#temp_array = #good_array.zip(#bad_array).flatten
[1,2,3].zip([3,4,5]).flatten # => [1, 3, 2, 4, 3, 5]
Here's what I think you're trying to do, though I'm not 100% certain:
a1 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
a2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
a1.zip(a2).each_with_index{|array, index| array.reverse! if index % 2 == 0}.map{|a| a.first}
# => ["a", "B", "c", "D", "e", "F", "g", "H", "i", "J"]
# Alternatively:
a1.zip(a2).each_with_index{|array, index| array.reverse! unless index % 2 == 0}.map{|a| a.first}
# => ["A", "b", "C", "d", "E", "f", "G", "h", "I", "j"]
I'm sure there's a shorter way to do it, but I can't think of it right now.
Edit: To answer your questions in the comment - both <% %> and <%= %> will run the ruby code they contain, but the second will output the response to the HTML, while the first one won't. Therefore, conditional statements are usually put in the first. So, for example, you could do:
<% if #user.admin? %>
<%= edit_link %>
<% else %>
You're not an admin!
<% end %>

Resources