Project

General

Profile

Actions

Bug #12710

closed

"a string X".index("a string") Fails with Ruby 2.2.3p173

Added by martin_vahi (Martin Vahi) over 7 years ago. Updated over 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
[ruby-core:77087]

Description

The test case:

#!/usr/bin/env ruby

s_0=`cd ..;pwd`.to_s
s_1=`cd . ;pwd`.to_s
x_0=s_1.index(s_0)
puts("s_0=="+s_0)
puts("s_1=="+s_1)
puts("x_0=="+x_0.to_s)
puts("x_0.class=="+x_0.class.to_s)

# The console output:
#
=begin
s_0==/home/ts2/tmp/xx10/aa AA
s_1==/home/ts2/tmp/xx10/aa AA/bb BB
x_0==
x_0.class==NilClass
=end
#
# The "ruby -v" gives:
=begin
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
=end


Files

rr.rb (402 Bytes) rr.rb martin_vahi (Martin Vahi), 08/28/2016 06:44 PM

Updated by martin_vahi (Martin Vahi) over 7 years ago

Interestingly, if the first s_0 assignment
of the Ruby script is changed to

s_0="X"+`cd ..;pwd`.to_s[0..(-2)]

then it seems to work.

Updated by lucasbuchala (Lucas Buchala) over 7 years ago

Isn't there a newline character at the end of string returned by shell quotes?

I think that's the reason the .index method will not find the substring.
If you don't want the newline, you can do something like:

`cmd ...`.chomp

So, probably, not a bug.

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Status changed from Open to Rejected

You can see invisible characters with p method or puts s_0.inspect.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0