*%w[..lib]

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

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”)

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

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.