Is it possible to write the action in two steps


#1

That's great and elegant. But (out of curiosity) is it possible to write the action in two steps, like this:

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

symbols = []

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

print symbols

I get the following print out:

["HTML", "CSS", "JavaScript", "Python", "Ruby"]nil

but with this error message "Oops, try again. It looks like HTML isn't a symbol." Anyone know why this is the case?


9 Converting Between Symbols and Strings
#2

This is not an in-place mutation. The above is only a representation that needs to be assigned, hence,

symbols.push(s.to_sym)

#3

Hello sir. I have not heard that term "in-place mutation" so far I believe. I am confused because I thought sprazzeus code would work to I am just wondering why. That method has always seemed to work before.


#4

in-place means to write the change back onto itself. Akin to,

s = s.to_sym

Consider string.capitalize...

s = s.capitalize

can be written as,

s.capitalize!

Note the !.


#5

A post was split to a new topic: Symbols.push(s)


Symbols.push(s)
#6