%w meaning in ruby

The %w notation in Ruby

The %w notation in Ruby is a shorthand way to create an array of strings. It allows you to create an array without using quotes or commas to separate the elements. Instead, you can simply list the elements separated by spaces, and Ruby will automatically convert them into an array of strings.

Here's an example to illustrate the usage of %w:

fruits = %w(apple banana orange)
puts fruits.inspect

Output:

["apple", "banana", "orange"]

In the example above, %w(apple banana orange) creates an array of strings with three elements: "apple", "banana", and "orange". The inspect method is used to display the contents of the array.

The %w notation is particularly useful when you want to create an array of strings without having to use quotes for each element. It provides a more concise and readable way to define arrays of strings.

[[SOURCE 10]]