# \*%w\[..lib\]

**URL:** <https://www.ruby-forum.com/t/w-lib/172441>\
**Category:** Ruby\
**Created:** [July 21, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/w-lib/172441 "2009-07-21T16:39:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsdickerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/34f0e0/32.png) [@wsdickerson](https://www.ruby-forum.com/u/wsdickerson)\
**Post date:** [July 21, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/w-lib/172441/1 "2009-07-21T16:39:35Z")

</div>

Hi All,

$:.unshift File.join(File.dirname( **FILE** ), \*%w[… lib])

what does \*%w mean in the line above?

Thanks for the help

the curious one

---

<div class="post-metadata">

**Author:** ![wsdickerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/34f0e0/32.png) [@wsdickerson](https://www.ruby-forum.com/u/wsdickerson)\
**Post date:** [July 21, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/w-lib/172441/2 "2009-07-21T17:11:48Z")

</div>

William Djingga wrote:

> Hi All,
> 
> $:.unshift File.join(File.dirname( **FILE** ), \*%w[… lib])
> 
> what does \*%w mean in the line above?

%w[… lib] creates an array of words. The result is ["…", “lib”]. The  
asterisk causes the array elements to be passed as separate arguments to  
File.dirname. It’s exactly equivalent to this (except it’s harder to  
understand):

$:.unshift File.join(File.dirname( **FILE** ), “…”, “lib”)

---

<div class="post-metadata">

**Author:** ![wsdickerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/34f0e0/32.png) [@wsdickerson](https://www.ruby-forum.com/u/wsdickerson)\
**Post date:** [July 21, 2009, 5:27pm UTC](https://www.ruby-forum.com/t/w-lib/172441/3 "2009-07-21T17:27:30Z")

</div>

Hi,

Am Dienstag, 21. Jul 2009, 23:39:38 +0900 schrieb William Djingga:

> $:.unshift File.join(File.dirname( **FILE** ), \*%w[… lib])
> 
> what does \*%w mean in the line above?

An array expands to an argument list.

%w() is just an array:

%w[foo bar baz] == [“foo”, “bar”, “baz”]

Here is an example for an \*argument list.

def f a, b, \*args  
puts args.inspect  
end

f( :x, :y, “i”, “j”)

# Output:

# [“i”, “j”]

ary = [“foo”, “bar”]  
f( :x, :y, \*ary)

# Output:

# [“foo”, “bar”]

So the call will expand to

$:.unshift File.join(File.dirname( **FILE** ), “…”, “lib”)

These will work as well:

ary = %w(a b c)  
[“foo”, “bar”, \*ary] #=\> [“foo”, “bar”, “a”, “b”, “c”]

case somestr  
when \*ary then do\_sth  
when \*%w(quit exit bye) then break  
end

Bertram

---

<div class="post-metadata">

**Author:** ![wsdickerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/34f0e0/32.png) [@wsdickerson](https://www.ruby-forum.com/u/wsdickerson)\
**Post date:** [July 21, 2009, 7:09pm UTC](https://www.ruby-forum.com/t/w-lib/172441/4 "2009-07-21T19:09:16Z")

</div>

On Wed, 22 Jul 2009 00:11:50 +0900, Tim H. wrote:

> understand):
> 
> $:.unshift File.join(File.dirname( **FILE** ), “…”, “lib”)

Which just comes to show you can write perl in any language.
