Can you guess

what this prints:

raise “NO FAIR RUNNING IT!”

class String
def expand(*args)
% args
end
end

template = “insert into foobar values(‘%s’, ‘%s’)”
puts(template.expand :foo, :bar)

if so, why?

-a

···

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

ahoward ahoward@fsl.noaa.gov wrote in news:Pine.LNX.4.53.0304261759300.2135@eli.fsl.noaa.gov:

class String
def expand(*args)
% args
end
end

it seems Ruby doesnt see it as

self % args

% args
is read as a string literal, ie.
% args <=> “args” <=> %{args} <=> %Q{args}
^ ^
spaces

Maybe spaces shouldn’t be allowed as delimiters.

···

On Sun, Apr 27, 2003 at 03:29:14AM +0900, ahoward wrote:

what this prints:

raise “NO FAIR RUNNING IT!”

class String
def expand(*args)
% args
end
end

template = “insert into foobar values(‘%s’, ‘%s’)”
puts(template.expand :foo, :bar)

if so, why?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Footnotes are for things you believe don’t really belong in LDP manuals,
but want to include anyway.
– Joel N. Weber II discussing the ‘make’ chapter of LPG

yes, that’s true. the question is, what does it see it as? the output
completely confuses me…

-a

···

On Sat, 26 Apr 2003, Marcin Miel¿yñski wrote:

ahoward ahoward@fsl.noaa.gov wrote in news:Pine.LNX.4.53.0304261759300.2135@eli.fsl.noaa.gov:

class String
def expand(*args)
% args
end
end

it seems Ruby doesnt see it as

self % args

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Mauricio Fernández batsman.geo@yahoo.com wrote in news:20030426185323.GA6800
@student.ei.uni-stuttgart.de:

% args
is read as a string literal, ie.
% args <=> “args” <=> %{args} <=> %Q{args}
^ ^
spaces

Maybe spaces shouldn’t be allowed as delimiters.

good idea! try this…

class String
def expand(*args)
%

  args
end

end

Sure, but then % is an infix operator. You wouldn’t expect “* 5” to behave
the same as “self * 5”, would you?

As a guess - is it anything to do with the various string notations like
%x{…} for execute etc? i.e.

%{args}

seems to give the same result.

Regards,

Brian.

···

On Sun, Apr 27, 2003 at 03:29:31AM +0900, Marcin Miel¿yñski wrote:

ahoward ahoward@fsl.noaa.gov wrote in news:Pine.LNX.4.53.0304261759300.2135@eli.fsl.noaa.gov:

class String
def expand(*args)
% args
end
end

it seems Ruby doesnt see it as

self % args

% args
is read as a string literal, ie.
% args <=> “args” <=> %{args} <=> %Q{args}
^ ^
spaces

not bad - it took me a few seconds to realize what was going on. you win -
er, let me get back to you on that one.

Maybe spaces shouldn’t be allowed as delimiters.

this seems quite reasonable - don’t think this would break any old code - at
least i hope not

-a

···

On Sun, 27 Apr 2003, Mauricio [iso-8859-1] Fernández wrote:

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Yes, as does
%;args;
You can use any character as a delimiter in Ruby, remember? So in the case of
% args
You’re using spaces as delimiters. I think. irb does some very strange things
in this case, depending on whether you put a space after the ‘args’ or not:

(Replace ‘_’ with space)
irb(main):027:0> class_String
irb(main):028:1> def_expand(*args)
irb(main):029:2> %args
irb(main):030:2> end
irb(main):031:1> end

Works fine.

irb(main):015:0> class_String
irb(main):016:1> def_expand(*args)
irb(main):017:2> %_args
irb(main):018:2" end
irb(main):019:2" end

Still thinks it’s inside the string, because I haven’t used a ‘closing space’
yet.

Tim Bates

···

On Sun, 27 Apr 2003 6:19 am, Brian Candler wrote:

As a guess - is it anything to do with the various string notations like
%x{…} for execute etc? i.e.

%{args}

seems to give the same result.


tim@bates.id.au

Hi –

As a guess - is it anything to do with the various string notations like
%x{…} for execute etc? i.e.

%{args}

seems to give the same result.

Yes, as does
%;args;
You can use any character as a delimiter in Ruby, remember? So in the case of
% args
You’re using spaces as delimiters. I think. irb does some very strange things
in this case, depending on whether you put a space after the ‘args’ or not:

(Replace ‘_’ with space)
irb(main):027:0> class_String
irb(main):028:1> def_expand(*args)
irb(main):029:2> %args
irb(main):030:2> end
irb(main):031:1> end

Works fine.

irb(main):015:0> class_String
irb(main):016:1> def_expand(*args)
irb(main):017:2> %_args
irb(main):018:2" end
irb(main):019:2" end

Still thinks it’s inside the string, because I haven’t used a
‘closing space’ yet.

That’s not strange though, is it? You’ve sort of contracted to
provide a delimiting space, so it’s just waiting for one. It’s
not irb-specific:

$ ruby -e 'puts(%_

···

On Sun, 27 Apr 2003, Tim Bates wrote:

On Sun, 27 Apr 2003 6:19 am, Brian Candler wrote:
abc
def
)’
_
abc
def

(also using _ for space) (though it would work with literal ‘_’ too
:slight_smile:

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

$ ruby -e 'puts(%_

abc
def
)’

Whoops, I mean:

_)’

David

···

On Sun, 27 Apr 2003 dblack@superlink.net wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav