* [ruby-core:82961] [Ruby trunk Bug#13937] ARGV doesn't behave like an array
[not found] <redmine.issue-13937.20170925032956@ruby-lang.org>
@ 2017-09-25 3:29 ` lucascaton
2017-09-25 3:51 ` [ruby-core:82962] [Ruby trunk Bug#13937][Rejected] " merch-redmine
1 sibling, 0 replies; 2+ messages in thread
From: lucascaton @ 2017-09-25 3:29 UTC (permalink / raw
To: ruby-core
Issue #13937 has been reported by lucascaton (Lucas Caton).
----------------------------------------
Bug #13937: ARGV doesn't behave like an array
https://bugs.ruby-lang.org/issues/13937
* Author: lucascaton (Lucas Caton)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Ruby allows us to access an array item by passing its index, regardless if there is or not a space between the array and the [] with the index.
Ie.: both of the following lines work:
~~~ ruby
[][0] # nil
[] [0] # nil
~~~
**ARGV** is an array as well. However, it doesn't work when we add a space to access some index:
~~~ ruby
ARGV # []
ARGV.class # Array
ARGV == [] # true
ARGV[0] # nil
ARGV [0] # NoMethodError: undefined method `ARGV' for main:Object
~~~
The last line should've returned **nil**.
Ps.: Same happens when you call a file with arguments by running **$ ruby file.rb foo bar**.
**ARGV [0]** will also raise an exception.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 2+ messages in thread
* [ruby-core:82962] [Ruby trunk Bug#13937][Rejected] ARGV doesn't behave like an array
[not found] <redmine.issue-13937.20170925032956@ruby-lang.org>
2017-09-25 3:29 ` [ruby-core:82961] [Ruby trunk Bug#13937] ARGV doesn't behave like an array lucascaton
@ 2017-09-25 3:51 ` merch-redmine
1 sibling, 0 replies; 2+ messages in thread
From: merch-redmine @ 2017-09-25 3:51 UTC (permalink / raw
To: ruby-core
Issue #13937 has been updated by jeremyevans0 (Jeremy Evans).
Status changed from Open to Rejected
lucascaton (Lucas Caton) wrote:
> Ruby allows us to access an array item by passing its index, regardless if there is or not a space between the array and the [] with the index.
> Ie.: both of the following lines work:
>
> ~~~ ruby
> [][0] # nil
> [] [0] # nil
> ~~~
>
> **ARGV** is an array as well. However, it doesn't work when we add a space to access some index:
>
> ~~~ ruby
> ARGV # []
> ARGV.class # Array
> ARGV == [] # true
> ARGV[0] # nil
> ARGV [0] # NoMethodError: undefined method `ARGV' for main:Object
> ~~~
>
> The last line should've returned **nil**.
>
> Ps.: Same happens when you call a file with arguments by running **$ ruby file.rb foo bar**.
> **ARGV [0]** will also raise an exception.
This isn't a bug, you just need to understand how the parser works.
`ARGV[0]` is parsed as `(ARGV)[0]`, a constant reference and then calling `[]` with the argument `0` on that reference. It's similar to `Object.const_get(:ARGV).public_send(:[], 0)`.
`ARGV [0]` is parsed as `ARGV([0])`, calling the `ARGV` method with a single argument being the array `[0]`. It's similar to to `send(:ARGV, [0])`. You get the NoMethodError here because no method named `ARGV` has been defined.
This issue isn't related to `ARGV` specifically, any constant or method called without arguments will have similar behavior:
~~~ruby
A = [1]
A[0] # 1
A [0] # NoMethodError
def a; [1] end
a[0] # 1
a [0] # ArgumentError
~~~
----------------------------------------
Bug #13937: ARGV doesn't behave like an array
https://bugs.ruby-lang.org/issues/13937#change-66861
* Author: lucascaton (Lucas Caton)
* Status: Rejected
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Ruby allows us to access an array item by passing its index, regardless if there is or not a space between the array and the [] with the index.
Ie.: both of the following lines work:
~~~ ruby
[][0] # nil
[] [0] # nil
~~~
**ARGV** is an array as well. However, it doesn't work when we add a space to access some index:
~~~ ruby
ARGV # []
ARGV.class # Array
ARGV == [] # true
ARGV[0] # nil
ARGV [0] # NoMethodError: undefined method `ARGV' for main:Object
~~~
The last line should've returned **nil**.
Ps.: Same happens when you call a file with arguments by running **$ ruby file.rb foo bar**.
**ARGV [0]** will also raise an exception.
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-09-25 3:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <redmine.issue-13937.20170925032956@ruby-lang.org>
2017-09-25 3:29 ` [ruby-core:82961] [Ruby trunk Bug#13937] ARGV doesn't behave like an array lucascaton
2017-09-25 3:51 ` [ruby-core:82962] [Ruby trunk Bug#13937][Rejected] " merch-redmine
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).