Ruby Strings
Ruby strings are Mutable, and created using single quote(‘ ‘) or double quote(” “) without mentioning the data types.
Example 1:
Ruby
# Ruby code implementation # for creation of Ruby Strings # using single quotes puts 'Ruby String using single quotes in GFG!' # using double quotes puts "Ruby String using double quotes in GFG!" # storing string into variables str = "Ruby String using variable in GFG!" # displaying string puts str |
Output:
Ruby String using single quotes in GFG! Ruby String using double quotes in GFG! Ruby String using a variable in GFG!
Double quotes interpolate the variables whereas single quotes can’t interpolate.
Example 2:
Ruby
# Ruby code implementation to # show single and double quotes str1 = 'Hello Geeks!' str2 = "Hello GFG" # using single quotes puts '#{str1}' puts '#{str2}' puts "#{str1}" puts "#{str2}" |
Output:
#{str1} #{str2} Hello Geeks! Hello GFG
Accessed by mentioning the index inside the square bracket[].
Example 3:
Ruby
# Ruby code implementation # for accessing of Ruby Strings str = "w3wiki" # accessing the specified substring puts str[ "Geeks" ] # print a single character from a # string using positive index puts str[ 2 ] # print a single character from a # string using negative index puts str[- 2 ] # print string in a range using comma(,) puts str[ 4 , 10 ] # print string in a range using(..) operator puts str[ 4 .. 8 ] |
Output:
Geeks e k sforGeeks sforG
To know more about please refer Ruby Strings article.
Ruby Containers
In Ruby, we can easily put method definition inside a container class, then create new objects by sending the new message to that class like most scripting languages. Ruby has a built-in module called Enumerable that’s included by Strings, Array, Hash, Range, and other classes that represent a collection of values. There are mainly four kinds of containers that represent a collection of data in Ruby:
- Strings: A string is a sequence or set of one or more characters.
- Arrays: Arrays are the collection of ordered, integer-indexed objects.
- Hashes: Hashes are collections of unique <key, value> pairs.
- It is often known as associative arrays, maps, or dictionaries.
- It is unordered.
- Ranges: Ranges depict a set of values between starting and endpoints. Values can be range, character, string, or objects.