I have a question about how to write the following code “nicer” without always having to initialize the variable (s = ''
) first.
a = ['H', 'e, 'l', 'l', 'o']
s = ''
a.each do { |i| s << i }
I have a question about how to write the following code “nicer” without always having to initialize the variable (s = ''
) first.
a = ['H', 'e, 'l', 'l', 'o']
s = ''
a.each do { |i| s << i }
Try using the reduce.
s = ['H', 'e', 'l', 'l', 'o'].reduce(''){|a, e| a << e}
a = ['H', 'e, 'l', 'l', 'o']
s = a.join
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs