Difference between %w and %W in Ruby

Learn via video courses
Topics Covered

Overview

Ruby provides two array literals, %w and %W, which allow developers to create arrays of strings quickly and efficiently. While they may appear similar, there are subtle differences between the two. This article aims to highlight the dissimilarities between %w and %W and provide examples of their usage.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Introduction

In Ruby, arrays are often used to store collections of similar data types, such as strings. To simplify the creation of string arrays, Ruby offers two different percent literals: %w and %W. These literals provide a concise and readable way to define arrays without explicitly using quotes and commas. However, understanding the disparity between %w and %W is crucial to utilize them effectively.

What is the Difference between %w and %W Array Literals in Ruby?

Sharpen Your Fundamentals with Free Learning

The Lower-Case %w Percent Literal

The %w literal in Ruby is utilized for creating an array of strings, where each string is separated by whitespace. It automatically converts the elements into strings. Special characters or interpolations are not allowed within the elements. The %w literal is commonly used when a simple array of strings needs to be created.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

The Upper-Case %W Percent Literal

The %W literal in Ruby serves a similar purpose to %w but offers added flexibility. It allows for string interpolation and the use of escape sequences within the elements of the array. This means that variables can be interpolated within the strings, and special characters such as newline (\n) and tab (\t) can be included. The %W literal is typically used when there is a need for string interpolation or special characters within the array elements.

Ruby developers can choose the appropriate array literal based on their specific requirements by understanding the distinctions between %w and %W. The %w literal is suitable for simple arrays of strings without special characters or interpolation. In contrast, the %W literal provides more flexibility, allowing for string interpolation and the inclusion of special characters within the array elements.

Examples

Let's explore some examples to illustrate the differences between %w and %W:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Using %w

Explanation

In this example, the %w literal is used to create an array of strings: "apple", "ball", and "cat". The whitespace separates each string, and the resulting array contains three elements.

Using %W

Explanation

In the given example, we demonstrate the utilization of the %W array literal in Ruby. This literal offers the advantage of string interpolation, allowing us to include the value of the variable name within the string and incorporate it into the final array. Additionally, we make use of the %W literal to enable the usage of escape sequences like \n, which represents newline characters. These escape sequences are properly incorporated within the resulting array.

Comparison between %w and %W

Explanation

We have a variable called name with the value "Rohit" in the above example. String interpolation does not occur when using the %w array literal. As a result, the array that results is ["Name", "=", "#name"]. Within the array, the variable name is handled as a string literal and is not interpolated.

When using the %W array literal with string interpolation, however, the variable name is interpolated within the array. This yields an array with the items ["Name", "=", "Rohit"]. During the interpolation procedure, the variable name is replaced with its equivalent value, "Rohit".

When it comes to string interpolation within array literals, this example clearly shows the difference between %w and %W. While %w interprets the interpolated variable as a literal string, %W conducts the actual interpolation and replaces the variable in the final array with its given value.

Conclusion

  • The %w and %W array literals in Ruby provide a convenient way to create string arrays.
  • %w creates an array of strings, separating elements by whitespace
  • %W also creates an array of strings but allows string interpolation and escape sequences.
  • %W is ideal for cases where string interpolation or special characters are required, providing more flexibility compared to %w.
  • %w is simpler and suitable for creating arrays of strings without any special characters or interpolation.
  • Understanding the differences between %w and %W enables developers to choose the appropriate literal based on their specific requirements.
  • By utilizing %w and %W effectively, developers can write more expressive and versatile code in Ruby.