0

I am currently working on the Ruby course in Codecademy and on the 'hashes and symbols' section. Here is the code I am working with:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

symbols = []

strings.each do |x|
    if x =='s'
        x.to_sym!
        symbols.push(x)
    end
end

The purpose of this code is to add the element of a string to the symbols variable if an 's' is encountered in a string. However, the code is not passing. I looked at a solution and found this:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

 symbols = []

 strings.each do |s|
     s= s.to_sym
     symbols.push(s)
end

My question is, does Ruby pick up on the actual 's' in the array. This seems like hard coding to me???

Pravesh Khatri
  • 2,124
  • 16
  • 22

3 Answers3

0

Codeacademy's solution you posted doesn't actually solve the problem you explained. An actual solution would look like this

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []

strings.each do |s|
    if s.downcase.include? 's'
        s= s.to_sym
        symbols.push(s)
    end
end
Will Sheehan
  • 25
  • 1
  • 5
0

I would write something like this:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = strings.select { |s| s =~ /s/i }.map(&:to_sym)
#=> [:CSS, :JavaScript]

Explanation:

select selects all elements of an array that need a condition. The condition is this example is: The string s should match the case-insensitive regexp /s/i (aka the string contains a 's' or 'S'). That said strings.select { |s| s =~ /s/i } would return ['CSS', 'JavaScript'].

map(&:to_sym) return the array, but called to_sym on each element first.

Another option might be:

symbols = strings.map { |s| s.to_sym if s =~ /s/i }.compact
spickermann
  • 100,941
  • 9
  • 101
  • 131
0

It seems as though you are actually trying to make each string into a symbol. If this is the case, the running an each method will simply overlook any 's''s. To see what's actually happening in the working solution, let's break your solution down first.

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each do |x|
#So, if x is equal to a Class String of S
    if x =='s'
#Turn that x into a symbol, which none of them should work
         x.to_sym!
         symbols.push(x)
    end
end

This is the code you say works.

 strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
 symbols = []
 #Similar each loop, but they used s and not x
 strings.each do |s|
 #Instead of checking whether there is a Class String of S, it is just
 #turning each value in the array into a symbol and pushing to the
 #symbols array
     s= s.to_sym
     symbols.push(s)
 end

Therefore, in summation, your code will never work unless there is an 's' in the array, which there may be s's but not a single string with an s. The latter works because it changes them all without an if statement.

-edit- I looked at the problem from Code Academy and I can see where the confusion came from, which is this instruction For each s in strings, use .to_sym to convert s to a symbol. This is somewhat confusing pseudocode, but let me reformat it so you can see where the confusion occurred strings.each |s| { s = s.to_sym } so the objective is to use s as your empty variable. You did not need to detect s, just use it as the empty variable.

Tommy Mertell
  • 105
  • 1
  • 8