Combining Array Values (strings) from two different arrays [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to arrays that I would like to combine. For example:
["a", "b", "c", "d"] is one array
["xxxx", "xx", "xxxxx", "x"] is another
My desired output would be a new array that would look like this:
["axxxx", "bxx", "cxxxxx", "dx"]
I'm not quite sure how to combine these two.
Much appreciated.

s = ["a", "b", "c", "d"].zip ["xxxx", "xx", "xxxxx", "x"]
#=> [["a", "xxxx"], ["b", "xx"], ["c", "xxxxx"], ["d", "x"]]
s.map &:join
# => ["axxxx", "bxx", "cxxxxx", "dx"]

Related

How to create nested arrays out of an existing array [closed]

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
If I have
arr = ["a", "b", "c", "d", "e", "f"]
how can I make arr equal to this?
[["a", "b"], ["c", "d"], ["e", "f"]]
the goal is to use arr.to_h to make it all into a hash. Thanks!
You can use Hash[]
Hash[*arr] #=> {"a"=>"b", "c"=>"d", "e"=>"f"}
Another option is Enumerable#each_slice
arr.each_slice(2).to_h #=> {"a"=>"b", "c"=>"d", "e"=>"f"}

How to print a Ruby Array on one line [closed]

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 2 years ago.
Improve this question
I have an array, and I need to print it out all on one line:
numbers = Array[[2, 4, 4, 5, 5, 7, 9]
Instead of separate lines how can I code this? I heard Array.join('') but I tried this code and it wouldn't run.
Always remember that the Ruby manual is your friend. If you read the manual for Array#join you will find some good candidates:
join(p1 = v1) public
Returns a string created by converting each element of the array to a string, separated by the given separator. If the separator is nil, it uses current $,. If both the separator and $, are nil, it uses an empty string.
[ "a", "b", "c" ].join #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"
So, now we can try:
numbers = [1, 2, 3, 4, 5]
puts numbers.join(" ")
Outputs:
1 2 3 4 5
Try
p numbers
This will print your array on a single line (which will obviously overflow depending on your window width).
You can use either method
puts numbers.join(', ');
or
puts numbers.inspect

How to convert every second element of an array into hash value [closed]

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
I have a string:
string = "a#b#c#d#e#f#g#h#...#z"
I want:
{:a => "b", :c => "d", :e => "f", ...}
First I split the string by doing:
array = string.split("#")
# => [a,b,c,d,e,f.....z]
Then I got stuck. Could anybody help?
Use each_slice to process pairs of elements from the array.
If the array has an even number of elements then calling to_h on the Enumerator returned by each_slice is enough to get the desired result:
string.split('#').each_slice(2).to_h
But to_h above fails if the last slice has only one item.
A general solution uses map to make sure the last slice always contains two items (the second being nil if needed), to prevent to_h fail:
string.split('#').each_slice(2).map{|a,b| [a.to_sym, b]}.to_h
Minor improvement of axiac's answer.
string.split("#").each_slice(2).with_object({}){|(k, v), h| h[k.to_sym] = v}
This does not create temporal arrays.
result_hash = Hash[*string.split("#")]
Here I have remove z from array to make an array with odd elements..
> array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"]
> Hash[ array.each_slice( 2 ).map { |e| e } ]
#=> {"a"=>"b", "c"=>"d", "e"=>"f", "g"=>"h", "i"=>"j", "k"=>"l", "m"=>"n", "o"=>"p", "q"=>"r", "s"=>"t", "u"=>"v", "w"=>"x", "y"=>nil}

Why does it keep saying that the function Split does not exist? [closed]

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?

How to collect different data from array or hash by ruby language? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example,I have datas:
a,b,c,a,c,d
I want to get different datas: a,b,c,d
I want to get data count: a => 2,b => 1,c => 2,d => 1
There may have other datas,such as e,f,g,etc.
How to do?
a = [:a,:b,:c,:a,:c,:d]
p h = a.each_with_object(Hash.new(0)){|i,h| h[i] += 1}
p h.keys
# >> {:a=>2, :b=>1, :c=>2, :d=>1}
# >> [:a, :b, :c, :d]

Resources