shell, ruby and regexp

U

Une bévue

over Mac OS X the latest and ruby 1.8.4 i've found strange interferences
between the shell (in my case zsh) and arguments to a ruby script if one
of the args is a Regexp as for examples :

/to.*/ - case 1
/^Tit.*/ - case 2


- case 1 (not the most seriaus) gave an answer from zsh BEFORE any
answer of my ruby script :

~/Desktop%> ruby test_args.rb /to.*/
zsh: no matches found: /to.*/

- case 2 seems to be DANGEROUS because it gaves me a kot of unexepected
args :

~/Desktop%> ruby test_args.rb /^Tit.*/
/Applications/
/Developer/
/Library/
/Network/
/System/
/Users/
/Volumes/
/automount/
/bin/
/cores/
/dev/
/etc/
/opt/
/private/
/sbin/
/tmp/
/usr/
/var/


Notice i've discovered that when designing a small script in order to
delete files )))

Obviously i've found a workaround using quotes around the regexp :

~/Desktop%> ruby test_args.rb "to.*"

but in that case i need another arg to my script in order to tell ruby
it is a regular expression.

did someone have the same quirk ?
 
U

Une bévue

Paul Lutus said:
What's wrong with the original string enclosed in quotes?

Instead of:

ruby test_args.rb /to.*/

Type this instead:

ruby test_args.rb "/to.*/"

ok i've done THE MISTAKE to rememeber zsh as a priority over ruby )))

my answer to "What's wrong with the original string enclosed in quotes?"

that didn't work, my script sees it as a string and even if (for this
arg and if enclosed with //) :

this_arg=Regexp.new(this_arg)

then if this arg = "/to.*/"

i get :

puts this_arg.source # (after Regexp.new)

# => /to.*/ INSTEAD OF to.*


the reason why i make no use of // for a regexp arg then i'm obliged to
have another arg yelling if this arg is or not a regexp.

may be here i've missed something too...
 
M

MonkeeSage

Une said:
that didn't work, my script sees it as a string and even if (for this
arg and if enclosed with //) :

this_arg=Regexp.new(this_arg)

then if this arg = "/to.*/"

i get :

puts this_arg.source # (after Regexp.new)

# => /to.*/ INSTEAD OF to.*

this_arg=Regexp.new(this_arg[1..-2])

Regards,
Jordan
 
E

Eero Saynatkari

--xUq7mlTLx96rFlBf
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
ok i've done THE MISTAKE to rememeber zsh as a priority over ruby )))
=20
my answer to "What's wrong with the original string enclosed in quotes?"
=20
that didn't work, my script sees it as a string and even if (for this
arg and if enclosed with //) :

Everything you get in ARGV is a String. You cannot create Ruby
objects outside Ruby.
this_arg=3DRegexp.new(this_arg)
=20
then if this arg =3D "/to.*/"
=20
i get :
=20
puts this_arg.source # (after Regexp.new)
=20
# =3D> /to.*/ INSTEAD OF to.*

You have a few choices. You can pass in '/to.*/' and strip off the /'s,
you can #eval it or you could pass in 'to.*' instead and then interpolate=
=20
or Regexp.new it.

--xUq7mlTLx96rFlBf
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFIhB97Nh7RM4TrhIRAllBAJ4/yPa4ozg9wnU7Y6wwn+3P7ASjgQCcChig
Aen8bQW/5AyUF1Be1s8EeAg=
=wsHh
-----END PGP SIGNATURE-----

--xUq7mlTLx96rFlBf--
 
M

Michael Fellinger

ok i've done THE MISTAKE to rememeber zsh as a priority over ruby )))

my answer to "What's wrong with the original string enclosed in quotes?"

that didn't work, my script sees it as a string and even if (for this
arg and if enclosed with //) :

this_arg=3DRegexp.new(this_arg)

then if this arg =3D "/to.*/"

i get :

puts this_arg.source # (after Regexp.new)

# =3D> /to.*/ INSTEAD OF to.*

Regexp.new('/.*rb/'[%r(/(.*?)/), 1])
# /.*rb/
 
R

Robert Klemme

Une bévue said:
ok i've done THE MISTAKE to rememeber zsh as a priority over ruby )))

It's not so much a "priority" as simple order of things happening (you can
read that on each shell's man page): first a command line undergoes various
substitutions (these change between shells and syntax used in the command
line), only *then* the result of these subsitutions is handed off to the
process forked (or shell internal command).
my answer to "What's wrong with the original string enclosed in
quotes?"

that didn't work, my script sees it as a string and even if (for this
arg and if enclosed with //) :

The point is, your script sees *every* argument as string. There is no
other type of arguments for process execs. If you need conversion, you have
to do it yourself. For example

ARGV.map! do |arg|
case arg
when %r{\A/.*/[io]*\z}
eval arg
when %r{[+-]?\d+}
arg.to_i
else
arg
end
end
may be here i've missed something too...

I guess you were mindset that there needs to be some mechanism to
automatically convert process arguments for you. That does not exist - at
least not in Ruby.

Kind regards

robert
 
U

Une bévue

Robert Klemme said:
ARGV.map! do |arg|
case arg
when %r{\A/.*/[io]*\z}
eval arg
when %r{[+-]?\d+}
arg.to_i
else
arg
end
end
may be here i've missed something too...

I guess you were mindset that there needs to be some mechanism to
automatically convert process arguments for you.

not exactly because i was doing a :

arg = Regex.new(arg)...
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top