Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert range to string?

Tags:

ruby

Is it possible to convert a range to a string? The idea is to make alpha = ("abcdefg..." etc). I want to do:

alpha.scan(/./) do |letters|
  puts "I have five vowels", if letters.include?("a", "e", "i", "o, "u")
end

Method 1:

alpha = ("a".."z").to_s # Still returns a range

Method 2:

alpha = *("a".."z").to_s # Same; returns a range
like image 783
Sylar Avatar asked Feb 02 '26 16:02

Sylar


1 Answers

Yes, try:

("a".."z").to_a.join # => => "abcdefghijklmnopqrstuvwxyz"
  1. You need to convert range to array with to_a.
  2. You can join the elements of the array.

Hope that helps!

like image 53
Paweł Dawczak Avatar answered Feb 04 '26 06:02

Paweł Dawczak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!