*%w[..lib]

W

William Djingga

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
 
T

Tim Hunter

William said:
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")
 
B

Bertram Scharpf

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
 
K

Ken Bloom

William said:
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")

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top