Is 'c' a character or string in Ruby - or both?

char hello[] = "hello"; #C hello = ['h', 'e', 'l', 'l', 'o'] #Ruby 

If I output the hello [0] class in Ruby, it says "String". This is because there are single string quotes in Ruby, and there seems to be no such thing as a char. The other day I told my colleague that he had a lot of characters, and he said: "No, no, I have a number of lines." Nitriki, yes, but technically, maybe heโ€™s right. Coming from the world of C, I usually donโ€™t think of a single character like String. Do you agree that the hello array above is an array of strings, not an array of characters?

+4
source share
4 answers

In C, a character is different from a string (which is an array of characters). Ruby is not individual. Lines can contain any number of characters, and Fixnums can hold the ASCII value for a character and convert to a print string containing that character using the #chr method.

The difference between the syntax of a single-quoted string and a double-quoted string in Ruby is related to how much preprocessing (e.g., interpolation) is performed on the string.

+13
source

Your colleague will be right, Ruby does not seem to have any character class.

 >> 'c'.class => String 
+2
source

Yes. Although in C, a string is represented as a C-String, which is basically an array of characters with a null character, String in Ruby is a class that stores its content in a more complex way. You can extract any part of it to a new line, and Ruby will probably not give you lower access to it. In C, you get direct access to its memory, Ruby is much less common than that.

+2
source

Yes, in Ruby there is no char class, only String exists. (Note that char is defined as 1 byte in the C standard, but this does not apply to Unicode characters).

0
source

All Articles