Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
a = '123712638126378123681273'
i have
=> a = ['12','37', ...... ]
I tried to use split but I do not know how to order it
help me
I would do:
a.scan(/\d{2}/)
#=> ["12", "37", "12", "63", "81", "26", "37", "81", "23", "68", "12", "73"]
Input
a = '123712638126378123681273'
Code
p a.gsub(/.{2}/).to_a
Or
p a.chars.each_slice(2).map(&:join)
Output
["12", "37", "12", "63", "81", "26", "37", "81", "23", "68", "12", "73"]
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a String like this in Ruby: "33255563"
Is it possible to create an Array like this: ["33", "2", "555", "6", "3"] ?
Try this
s.each_char.chunk_while(&:==).map(&:join)
=> ["33", "2", "555", "6", "3"]
Input
a="33255563"
Program
p a.chars.slice_when{|a,b|a!=b}.map(&:join)
Output
["33", "2", "555", "6", "3"]
str = "33255563"
str.gsub(/(.)\1*/).to_a
#=> ["33", "2", "555", "6", "3"]
This uses String#gsub with one argument and no block, which returns an enumerator. This form of gsub performs no substitutions; it merely generates matches of the given pattern, which here is a regular expression.
We can write the regular expression in free-spacing mode to make it self-documenting.
/
(.) # match any character and save to capture group 1
\1* # match the content of capture group 1 zero or more times,
# as many as possible
/x # invoke free-spacing regex definition mode
Note that String#scan cannot be used because the regular expression contains a capture group.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
x = ["1", "2", "3", "4"]
#=> ["1", "2", "3", "4"]
x.split(", ")
#=> NoMethodError: undefined method `split' for ["1", "2", "3", "4"]:Array
The String#split method in ruby is used to divide a string into substrings
'a,b,c,d'.split(',') # => ["a", "b", "c", "d"]
You are trying to invoke Array#split (aka on an array object). As such array method doesn't exists, you get:
error undefined method split for ["1", "2", "3", "4"]:Array`
Arrays can't be split. You are probably thinking of splitting a string?
You will reach the desired value by simply x[0] for the 0 index, first object in the array.
This to any help?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I currently want to get the numbers from 1 to 23 as a string.
The method I arrived at feels slightly hard to read:
CHROMOSOME_NUMBERS = (1..23).to_a.map { |n| n.to_s }
Is there a prettier way of doing it?
There is map method defined for Range (because Range includes Enumerable module), so you don't have to convert it manually to array:
CHROMOSOME_NUMBERS = (1..23).map(&:to_s)
('1'..'23').to_a
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like this:
["1", "3", "2"]["2", "3", "1"]["3", "1", "2"]...
And want to transform it to an array that looks like this:
[132][231]..
What can i do? Thanks!
Using Enumerable#map, Array#join, and String#to_i:
a = ["1", "3", "2"],["2", "3", "1"],["3", "1", "2"]
a.map { |x| x.join.to_i } # => [132, 231, 312]
a.map { |x| [x.join.to_i] } # => [[132], [231], [312]]
a = [["1", "3", "2"],["2", "3", "1"],["3", "1", "2"]]
a.map{|e| [e.join.to_i]}
# => [[132], [231], [312]]
Ensure yourself that your variable has correct format and then as said
a = [["1", "3", "2"],["2", "3", "1"],["3", "1", "2"]]
a.map do |x|
x.join.to_i
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this array of arrays:
[["abc", "123"], ["cde", "456"], ["cde", "674"]]
And I want this array of arrays arranged in this way:
{ "name": "test", "children": [ {"name": "abc", "children": [ {"name": "123"} ]}, {"name": "cde", "children": [ { "name": "456"},{"name": "674"} ]}]}
How can I make this transformation in ruby language?
Thanks in advance.
Try this
require 'json'
src_arr= [["abc", "123"], ["cde", "456"], ["cde", "674"]]
tmp = {} # to collect all common node first
src_arr.each do |arr|
if node = tmp[arr.first] # check if node exists
node['children'] << {'name' => arr.last} # append of exists
else
# add node if does not exists
tmp[arr.first] = {'name' => arr.first,'children' => [{'name' => arr.last}]}
end
end
tree = {'name' => 'test','children' => tmp.values}
puts tree
#=> {"name"=>"test", "children"=>[{"name"=>"abc", "children"=>[{"name"=>"123"}]}, {"name"=>"cde", "children"=>[{"name"=>"456"}, {"name"=>"674"}]}]}
puts JSON.generate(tree)
#=> {"name":"test","children":[{"name":"abc","children":[{"name":"123"}]},{"name":"cde","children":[{"name":"456"},{"name":"674"}]}]}