String in Ruby

2020-12-16 hit count image

Let's see how to use the String in Ruby.

Outline

In this blog post, I will introduce how to use the String in Ruby.

In Ruby, you can search the string by the index method.

puts "ABCD".index("C")
# 2

If the searched string is in the string, the index method returns the index of the string position. If the searched string doesn’t exist, nil is returned.

Replace

we can refer to the string via index, and replace the string via index.

a = "Apple Juice"
a[0] = "O"
a[1] = "r"
a[2] = "a"
a[3] = "n"
a[4] = "ge"
puts a
# Orange Juice

As you see above, we can replace between the different the length of the strings like a[4] = "ge".

Regular expression

Like other languages, you can use the regular expression to search the string in Ruby.

Meta characters

In the regular expression, we use the meta characters to make the regular expression.

Meta charactersDescription
[]single character in []
.any one character
*repeat the last expression more than zero times.
+repeat the last expression more than once.
?repeat the last expression more than zero times or once.
()regular expression group
a|bone character in a, b
\sspace character
\walphanumeric or underbar(_)
\dnumber(0~9)
^the front of the string
$the end of the string

Pattern matching

You can search the string by the regular expression.

puts /R.by/ =~ "Let's study Ruby!"
# 12

Like the index method, if the pattern is matched, the index of the string is returned. if the pattern is not matched, nil is returned.

Regexp class

You can make the regular expression object by the new method of the Regexp like below.

re = Regexp.new("R.by")
puts re =~ "Let's study Ruby!"
# 12

Regular expression objects

You can use more efficiently the regular expression with the regular expression objects.

  • Regular expression object x: If you use x, you can add the comments in the regular expression.

      re =/R.by # Search Ruby/x
      puts re =~ "Let's study Ruby!"
      # 12
    
  • Regular expression object i: If you use i, you can search the string without case-sensitive.

      re =/r.by/i
      puts re =~ "Let's study Ruby!"
      # 12
    

Symbol

The Symbol is a class that identifies the variable. It is like the memory address of the variables.

a = :apple
b = :apple
c = :orange

puts a == b # true
puts a == c # false

In the above, a and b is the same symbol, so it means the same objects.

Symbol and String

You can convert the Symbol to the String by to_s, and the String to the Symbol by intern.

a = :apple
b = "apple"

puts a # apple
puts b # apple
puts a == b # false
puts a.to_s == b # true
puts a == b.intern # true

Usages of the Symbol

The Symbol is like the memory address, so it’s faster and better for the memory that using the String.

def triangle(value)
  value[:a] + value[:b] + value[:c]
end

test = {:a => 5, :b => 2, :c => 5}
puts triangle(test)
# 12

The Symbol is normally used for the Hash keys.

Percent technique

Normally, the String is in the double quotation(“) or single quotation(‘), but we can use % for it.

puts %{This string has " character}
# This string has " character
puts %Q{This string has " character}
# This string has " character
puts %q{This string has ' character}
# This string has ' character

Also, we can convert the string includes the space divisions to the array by %w.

puts %w(apple orange banana)
# apple
# orange
# banana

Completed

We’ve seen how to use the string in Ruby. The regular expression is used in other languages, so if you remember it, you can use it in many places.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts