Combining symbols in ruby

ruby symbols
ruby symbols vs strings
ruby hash
symbol in rails
ruby array
ruby symbol hash
ruby compare symbols
ruby symbolize

I have two arrays, one with symbols and another with strings.

a = [:man, :woman]
b = ["one", "two"]

I'm trying to combine every symbol with each string in the array so that the output would be:

[:man_one, :man_two, :woman_one, :woman_two]

I've tried

b = b.to_s
q = []
a.each do |n|
  q.push (n.to_s + b.each {|w| "_" + w}).to_sym
end
p q

But this gives me an error. How can I create the new symbols?

A straightforward way is this:

a.product(b).map{|arr| arr.join("_").to_sym}
#=> [:man_one, :man_two, :woman_one, :woman_two]

Ruby Symbols: Ruby Study Notes, What is the difference between symbol and string in Ruby? A symbol is written like this: :something. I.e. there is a word that is preceded by a colon. This means that normally symbols do not contain spaces. Instead, if we have symbols that consist of multiple words we would concatenate them with underscores, like so: :another_funny_symbol. A symbol is created by adding a colon in front of a word.

You have some problems while trying to achieve your expected output:

Converting b to String gives you nothing else than "[\"one\", \"two\"]", which isn't iterable, so I guess that mess everything.

In the other hand it seems you're trying to iterate on a, to iterate on b then, but pushing to q right the value of the elements of a to String plus a call on each to b, seems to give you nothing.

With a slight tweak you can make that work:

a = [:man, :woman]
b = ["one", "two"]
q = []

a.each do |n|
    b.each do |w|
        q << ("#{n}_#{w}").to_sym
    end
end

p q
# [:man_one, :man_two, :woman_one, :woman_two]

String concatenation in Ruby, I have two arrays, one with symbols and another with strings. a = [:man, :woman] b = ["one", "two"]. I'm trying to combine every symbol with each string in the  A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. In this tutorial, you'll learn how to work with strings in Ruby. You'll create strings, display them on the screen, store them in variables, join multip

Yet another riff on the nested loop without mutating an external variable within the loops

a = [:man, :woman]
b = ["one", "two"]

q = a.flat_map do |n|
  b.map do |w|
    "#{n}_#{w}".to_sym
  end
end

p q 

How to Combine Arrays in Ruby, If it's code, then use a symbol, especially when used as keys in hashes (see below). Another way of looking at symbols is that they aren't really text, even though  Symbol GC (Advanced) Another interesting fact about symbols is that there are different types. The reason is that symbols were not garbage collected before Ruby 2.2, which means that they where not cleaned up from memory when no longer needed like regular Ruby objects (strings, hashes, arrays…).

Look at Array#product

a = [:man, :woman]
b = ["one", "two"]
a.product(b).map { |arr| arr.join('_') }.map(&:to_sym)

Your error is because you're trying to call each on a String (b), because you converted it with to_s a few lines up.

If you want to do this with the temporary variable, q, you can write it like this:

a=[:man, :woman ]
b=["one", "two"]
q=[]

a.each do |tmp_a|
  b.each do |tmp_b|
    q.push((tmp_a.to_s + '_' + tmp_b).to_sym)
  end
end

puts q

Ruby Symbols vs. Strings - Lindsay Criswell, Symbols are tiny lightweight Ruby placeholder objects. Symbols often seem like magic runes to Ruby newcomers. We can use zip to combine two arrays. Symbols can be confusing for newbie Ruby programmers. You will see Symbols used a lot in Ruby, and so it’s important to really understand why they are used. Just remember, a Symbol is basically just a string that can’t be changed. This is perfect for identifying things like key’s in a hash that don’t need the overhead of a String object.

Just to give another example of https://ruby-doc.org/core-2.5.3/Array.html#method-i-product, passing a block:

res = []
a.product(b){ |e| res << e.join('_').to_sym }
res #=> [:man_one, :man_two, :woman_one, :woman_two]

Combining symbols in ruby, How can you combine two or more strings together? What if you want to merge a variable, or even numbers into another string? Use the concat method & string  a string containing my_str will be created twice, and x and y will point to different instances. As a result, symbols are often used as the equivalent to enums in Ruby, as well as keys to a dictionary (hash).

Symbols, The whole point of the article is that Ruby already has immutable strings that are basically identical to symbols, so it would be easy to remove symbols but keep  Ruby supports a rich set of operators, as you'd expect from a modern language. Most operators are actually method calls. For example, a &plus; b is interpreted as a.&plus;(b), where the &plus; method in the object referred to by variable a is called with b as its argument.

Ruby Symbols and Hashes, I'm trying to merge to hashes, one using symbols as keys (the defined default values for my class) and the other using strings as keys (taken  Ruby Stones Properties Rubies are one of the most precious stones in the entire world. They are among the most highly valued stones in modern society, alongside diamonds, emeralds, and sapphires. An interesting fact, however, is that ruby

String Concatenation & Interpolation in Ruby (With Examples), If Ruby were to automatically convert things to Symbols for you then yes, but in this case frameworks such as Rails are the cause of the problem. Merging the two  Ruby Symbolism and Blood. Ruby’s blood-like color no doubt encouraged strong associations with this life-sustaining fluid. Those who risked their lives were believed to have a special connection to the gem. The ancient Burmese prized the ruby as the stone of soldiers. They believed it bestowed invulnerability.

Comments
  • Alternatively a.product(b).map { |g, n| :"#{g}_#{n}" } – rarely seen, but interpolation also works with symbol literals.
  • @Stefan Opps. I will fix it.
  • What are you trying to show using permutation? It raises an error.
  • I mean to write "product", but I was looking at the permutation documentation when i wrote this and didn't go back and test it. The link went to the right documentation, but nothing else was right :) Good catch, I updated the answer