Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

What is the string.inspect method in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Ruby, the inspect string method is used to return a printable version of a string that is surrounded by quotes. If there are any special characters present in the string, they will be escaped.

Syntax

str.inspect

Parameters

  • str: The string that we want to inspect.

Return value

This method returns a string that is surrounded by quotation marks.

Code example

# create strings
str1 = "Edpresso"
str2 = "is"
str3 = "awesome"
# inspect strings
a = str1.inspect
b = str2.inspect
c = str3.inspect
# print results
puts a
puts b
puts c
Add quotes to a string using the "inspect" method in Ruby

Code explanation

  • Lines 2–4: We create some strings and initialize them with values.
  • Lines 7–9: We call the inspect method on the strings that we created and store the results in the variables a, b, and c.
  • Lines 12–14: We print the results to the console.

RELATED TAGS

ruby
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring