Ruby + json + line counting

Greetings, all.

I stumbled across some odd behaviour between ruby 1.9.3 and ruby
2.2.2, and was wondering if it is something within my setup or an
"undocumented" oddity.

Here is the code and output when run on ruby 1.9.3:

$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
$ echo hello | ruby -rjson -lane 'print $., $_'
1hello

Here is the same code when run on ruby 2.2.2:

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin12.0]
$ echo hello | ruby -rjson -lane 'print $., $_'
4hello

Why is ruby 2.2.2 generating "4hello" while 1.9.3 generates "1hello"?

Regards,
- Robert

Something about -rjson causes this:

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-cygwin]
$ echo hello | ruby -lane 'BEGIN {p $.}; p $., $_, $F'
0
1
"hello"
["hello"]
$ echo hello | ruby -rjson -lane 'BEGIN {p $.}; p $., $_, $F'
3
4
"hello"
["hello"]

The splitting does not change but the line counter. Odd.

Cheers

robert

···

On Wed, Aug 12, 2015 at 1:21 AM, Robert Citek <robert.citek@gmail.com> wrote:

Greetings, all.

I stumbled across some odd behaviour between ruby 1.9.3 and ruby
2.2.2, and was wondering if it is something within my setup or an
"undocumented" oddity.

Here is the code and output when run on ruby 1.9.3:

$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
$ echo hello | ruby -rjson -lane 'print $., $_'
1hello

Here is the same code when run on ruby 2.2.2:

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin12.0]
$ echo hello | ruby -rjson -lane 'print $., $_'
4hello

Why is ruby 2.2.2 generating "4hello" while 1.9.3 generates "1hello"?

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can -
without end}
http://blog.rubybestpractices.com/

Thanks for confirming.

The phenomenon seems to be not limited to the JSON module, but affects
a few others, too.

Here's a script to check all modules in my LOAD_PATH:

ruby -e '$LOAD_PATH.each{|x| system("ls #{x} 2> /dev/null") }' |
ruby -F'\.' -lane 'print $F[0] if ~ /.rb$/' |
while read r ; do
  echo $r $(echo | ruby -r${r} -lane 'print $.' 2> /dev/null ) | grep -v 1$
done

Output:

json 4
psych 4
rake 4
rdoc 4
rss 4
yaml 4

As before, this is seen on 2.2.2 but not on 1.9.3.

Regards,
- Robert

···

On Wed, Aug 12, 2015 at 5:11 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Wed, Aug 12, 2015 at 1:21 AM, Robert Citek <robert.citek@gmail.com> > wrote:

Greetings, all.

I stumbled across some odd behaviour between ruby 1.9.3 and ruby
2.2.2, and was wondering if it is something within my setup or an
"undocumented" oddity.

Here is the code and output when run on ruby 1.9.3:

$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
$ echo hello | ruby -rjson -lane 'print $., $_'
1hello

Here is the same code when run on ruby 2.2.2:

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin12.0]
$ echo hello | ruby -rjson -lane 'print $., $_'
4hello

Why is ruby 2.2.2 generating "4hello" while 1.9.3 generates "1hello"?

Something about -rjson causes this:

$ ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-cygwin]
$ echo hello | ruby -lane 'BEGIN {p $.}; p $., $_, $F'
0
1
"hello"
["hello"]
$ echo hello | ruby -rjson -lane 'BEGIN {p $.}; p $., $_, $F'
3
4
"hello"
["hello"]

The splitting does not change but the line counter. Odd.

Cheers

robert

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can -
without end}
http://blog.rubybestpractices.com/