Tutorial by Examples

The main difference is that double-quoted String literals support string interpolations and the full set of escape sequences. For instance, they can include arbitrary Ruby expressions via interpolation: # Single-quoted strings don't support interpolation puts 'Now is #{Time.now}' # Now is #{Time...
Ruby provides several ways to create a String object. The most common way is using single or double quotes to create a "string literal": s1 = 'Hello' s2 = "Hello" The main difference is that double-quoted string literals are a little bit more flexible as they support interpo...
Concatenate strings with the + operator: s1 = "Hello" s2 = " " s3 = "World" puts s1 + s2 + s3 # => Hello World s = s1 + s2 + s3 puts s # => Hello World Or with the << operator: s = 'Hello' s << ' ' s << 'World' puts s # => ...
The double-quoted delimiter " and %Q sequence supports string interpolation using #{ruby_expression}: puts "Now is #{Time.now}" # Now is Now is 2016-07-21 12:47:45 +0200 puts %Q(Now is #{Time.now}) # Now is Now is 2016-07-21 12:47:45 +0200
"string".upcase # => "STRING" "STRING".downcase # => "string" "String".swapcase # => "sTRING" "string".capitalize # => "String" These four methods do not modify the original receiver. For exampl...
String#split splits a String into an Array, based on a delimiter. "alpha,beta".split(",") # => ["alpha", "beta"] An empty String results into an empty Array: "".split(",") # => [] A non-matching delimiter results in an Array...
Array#join joins an Array into a String, based on a delimiter: ["alpha", "beta"].join(",") # => "alpha,beta" The delimiter is optional, and defaults to an empty String. ["alpha", "beta"].join # => "alphabeta" An em...
The easiest way to create a multiline string is to just use multiple lines between quotation marks: address = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.&quot...
Ruby can inject an array of values into a string by replacing any placeholders with the values from the supplied array. "Hello %s, my name is %s!" % ['World', 'br3nt'] # => Hello World, my name is br3nt! The place holders are represented by two %s and the values are supplied by the...
The tr method returns a copy of a string where the characters of the first argument are replaced by the characters of the second argument. "string".tr('r', 'l') # => "stling" To replace only the first occurrence of a pattern with with another expression use the sub method ...
In Ruby, a string is just a sequence of bytes along with the name of an encoding (such as UTF-8, US-ASCII, ASCII-8BIT) that specifies how you might interpret those bytes as characters. Ruby strings can be used to hold text (basically a sequence of characters), in which case the UTF-8 encoding is us...
p "This is %s" % "foo" # => "This is foo" p "%s %s %s" % ["foo", "bar", "baz"] # => "foo bar baz" p "%{foo} == %{foo}" % {:foo => "foo" } # => "foo == foo" See String...
To find if a string starts with a pattern, the start_with? method comes in handy str = "zebras are cool" str.start_with?("zebras") => true You can also check the position of the pattern with index str = "zebras are cool" str.index("zebras").zero...
To find if a string ends with a pattern, the end_with? method comes in handy str = "I like pineapples" str.end_with?("pineaaples") => false
In Ruby, strings can be left-justified, right-justified or centered To left-justify string, use the ljust method. This takes in two parameters, an integer representing the number of characters of the new string and a string, representing the pattern to be filled. If the integer is greater than th...

Page 1 of 1