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"]
Related
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"]
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 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
Here are two range values.
('1'..'10').to_a => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
but,
('2'..'10').to_a => []
Why does the second one return an empty array in ruby 1.8.7?
The issue is that Ruby 1.8 is trying to determine whether ('2'..'10') is a valid range as part of its analysis of the code, and deciding that it's invalid. '2' sorts after '10' making the range values backwards.
This is a common problem when people try to sort string values, and they'll encounter it again and again until they learn that trick never works.
Using 1.8.7:
('2'..'10').to_a
=> []
('02'..'10').to_a
=> ["02", "03", "04", "05", "06", "07", "08", "09", "10"]
Or:
(2..10).map(&:to_s)
=> ["2", "3", "4", "5", "6", "7", "8", "9", "10"]
I'd probably use the last method if I needed compatibility with 1.8 and 1.9+ Rubies.
irb(main):001:0> '2' > '10'
=> true
irb(main):002:0> '1'>'10'
=> false
Because of the string comparison algorithm, range cannot be created with lower bound bigger than the upper bound.
ACTUALLY it works in 1.9.3. Strange.
That's not what I'm getting:
irb(main):001:0> ('2'..'10').to_a
=> ["2", "3", "4", "5", "6", "7", "8", "9", "10"]
Unrelated to the question, though: the correct way is:
(2..10).to_a
(I am using Ruby 1.9.3)
in ruby2.0.0-p-0 this is what I am getting :
["2", "3", "4", "5", "6", "7", "8", "9", "10"]