-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.rb
More file actions
21 lines (17 loc) · 1.21 KB
/
Copy pathstring.rb
File metadata and controls
21 lines (17 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# String
# In Ruby, a string is a sequence of characters enclosed in either single quotes (' ') or double quotes (" ").
# Strings are used to represent text and can contain letters, numbers, symbols, and whitespace.
# Example of string literals
greeting = "Hello, World!" # Using double quotes
name = 'Luthfi' # Using single quotes
# You can perform various operations on strings, such as concatenation, interpolation, and manipulation.
# Concatenation is the process of combining two or more strings together using the `+` operator.
full_greeting = greeting + " My name is " + name + "." # Concatenation
puts full_greeting # Output: Hello, World! My name is Luthfi.
# String interpolation allows you to embed variables directly within a string.
# To use string interpolation, you need to wrap the variable in `#{}` within a double-quoted string.
interpolated_greeting = "#{greeting} My name is #{name}." # String interpolation
puts interpolated_greeting # Output: Hello, World! My name is
# concat method is used to concatenate strings in place, it modifies the original string.
greeting.concat(" My name is #{name}.") # Using concat method (will effect the original string)
puts greeting # Output: Hello, World! My name is Luthfi.