How do I test if variable Name starts with Alphabet or not?


I need to check if a variable “name” starts with a variable or not, how
do i do that?

Essential, i want to sort array of names alphabetically : i was using
names.sort_by{|c| "c[‘name’]}

But if name has numerical or special characters, they score well in
sorting and are there in the beginning of the sorted list. But I want
those special names to be in the end. How do i do this? Please suggest


If I understand correctly, you have letters and numbers in your array?
Something like:

[“a”, “b”, 23, 1]

I’m just a beginner in Ruby and I know it’s not the best way to do it,
but here’s what I would do. I would separate the array into two
different arrays (e.g., numbers and strings), sort them separately and
then concatenate the arrays.

[“a”, “b”, 23, 1].each { |x| if x.is_a?(String) then strings << x else
numbers << x end }
result = strings.sort + numbers.sort

Something like that? If your array members are strings with numbers
embedded in them, then use regular expressions to separate them into
the two arrays. Hope I’m not way off the mark.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs