Explanation about Ruby String object

Let's take a look at Ruby's String object using irb.

What is a String object?

An object that handles character strings. Enclose it in single or double quotes to create a String object. Let's check with the class method.

 $ irb
irb(main):001:0> "String".class
=> String

Function of String object

Here are some commonly used methods for String objects.

Uppercase with upcase method

irb(main):002:0> 'String'.upcase
=> "STRING"

Lowercase with downcase method

irb(main):007:0> 'String'.downcase
=> "string"

Cut out characters

You can cut out the specified character string by using the slice method. In the example below, it is cut out from the 0th character to 2 (one before 3).

irb(main):006:0> 'String'.slice(0, 3)
=> "Str"

Check size

irb(main):014:0> 'String'.size
=> 6

Object type conversion

Conversion to integer

Use a method called to_i.

irb(main):018:0> '10000'.class
=> String
irb(main):019:0> '10000'.to_i.class
=> Integer

Conversion to float

Use a method called to_f.

irb(main):020:0> '1.1'.class
=> String
irb(main):021:0> '1.1'.to_f.class
=> Float

Working with String objects

Concatenation of characters

irb(main):027:0> 'Str' + 'ing'
=> "String"

Concatenate characters at the end

irb(main):030:0> 'String' << '!'
=> "String!"

You can also concatenate continuously by using variables.

irb(main):038:0> str = 'String'
=> "String"
irb(main):039:0> str << '!'
=> "String!"
irb(main):040:0> str << '!'
=> "String!!"

Self-substitution

irb(main):042:0> str = 'String'
=> "String"
irb(main):043:0> str += '!'
=> "String!"

Expand variables in strings

If you embed the expression # {variable} in a string enclosed in double quotes Variables can be expanded in strings.

irb(main):047:0> name = 'Masuyama'
=> "Masuyama"
irb(main):048:0> "my name is#{name}is"
=> "My name is Masuyama"

By the way, not only variable expansion but also expression embedding and expansion is possible.

irb(main):052:0> "1 + 1 = #{1+1}"
=> "1 + 1 = 2"

Recommended Posts

Explanation about Ruby String object
Explanation about Ruby Range object
Explanation about Array object of Ruby
About Ruby and object model
About Ruby symbols
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About object orientation
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About Ruby exception handling
About Ruby Hashes (continued)
About eval in Ruby
[ruby] About here documents
About Ruby if statement
Ruby Learning # 31 Object Methods
About Ruby instance methods
About Java String class
[Ruby] About instance generation
About the [ruby] operator
Thinking about logic Ruby
Various Ruby string operations
Convert to Ruby Leet string
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
[Ruby] About the difference between 2 dots and 3 dots of range object.
Ruby About various iterative processes
Ruby Learning # 8 Working With String
About the explanation about functional type
About Ruby classes and instances
Cut out a Ruby string
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
About the behavior of ruby Hash # ==
String comparison without worrying about NullPointerException
Convert ruby object to JSON format
About Ruby single quotes and double quotes
Is String # format (String, Object ...) really slow?
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each