ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:54234] [ruby-trunk - Bug #8262][Open] Confusing "undefined method" exception when using - operator on a string
@ 2013-04-12 16:17 ajack (Jack Weeden)
  2013-04-12 18:51 ` [ruby-core:54240] [ruby-trunk - Bug #8262] " marcandre (Marc-Andre Lafortune)
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: ajack (Jack Weeden) @ 2013-04-12 16:17 UTC (permalink / raw
  To: ruby-core


Issue #8262 has been reported by ajack (Jack Weeden).

----------------------------------------
Bug #8262: Confusing "undefined method" exception when using - operator on a string
https://bugs.ruby-lang.org/issues/8262

Author: ajack (Jack Weeden)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 
ruby -v: 1.9.3


When trying to use the minus operator (-) on a String, Ruby reports the following exception:

> -"5"
> NoMethodError: undefined method `-@' for "5":String

I'm not sure if this is intended behaviour but it was confusing as it led me to believe I was trying to call `-@"5"` rather than `-"5"` (I realise the minus operator is invalid for a String but I was looking all over for somewhere I'd accidentally typed in an "@" symbol, rather than somewhere I was just trying to call `-"5"`)

Applies to Ruby 1.8.7, 1.9.3 and 2.0.0



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:54240] [ruby-trunk - Bug #8262] Confusing "undefined method" exception when using - operator on a string
  2013-04-12 16:17 [ruby-core:54234] [ruby-trunk - Bug #8262][Open] Confusing "undefined method" exception when using - operator on a string ajack (Jack Weeden)
@ 2013-04-12 18:51 ` marcandre (Marc-Andre Lafortune)
  2013-04-18 17:10 ` [ruby-core:54434] [ruby-trunk - Feature " ajack (Jack Weeden)
  2013-04-18 17:22 ` [ruby-core:54435] [ruby-trunk - Feature #8262][Closed] " marcandre (Marc-Andre Lafortune)
  2 siblings, 0 replies; 4+ messages in thread
From: marcandre (Marc-Andre Lafortune) @ 2013-04-12 18:51 UTC (permalink / raw
  To: ruby-core


Issue #8262 has been updated by marcandre (Marc-Andre Lafortune).


The thing is that the name of the "unary minus" method is "-@", and what you did was send that to "5".

    42.send(:-@)  # => -42 
    "5".send(:-@) # => same as -"5", i.e. NoMethodError: undefined method `-@' for "5":String

I agree it's not super clear, but it is correct and has the advantage of teaching people what the method for unary minus is, if they wanted to define one for example.

What would you have liked to read instead as an error message?

(moving to feature)
----------------------------------------
Bug #8262: Confusing "undefined method" exception when using - operator on a string
https://bugs.ruby-lang.org/issues/8262#change-38503

Author: ajack (Jack Weeden)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 
ruby -v: 1.9.3


When trying to use the minus operator (-) on a String, Ruby reports the following exception:

> -"5"
> NoMethodError: undefined method `-@' for "5":String

I'm not sure if this is intended behaviour but it was confusing as it led me to believe I was trying to call `-@"5"` rather than `-"5"` (I realise the minus operator is invalid for a String but I was looking all over for somewhere I'd accidentally typed in an "@" symbol, rather than somewhere I was just trying to call `-"5"`)

Applies to Ruby 1.8.7, 1.9.3 and 2.0.0



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:54434] [ruby-trunk - Feature #8262] Confusing "undefined method" exception when using - operator on a string
  2013-04-12 16:17 [ruby-core:54234] [ruby-trunk - Bug #8262][Open] Confusing "undefined method" exception when using - operator on a string ajack (Jack Weeden)
  2013-04-12 18:51 ` [ruby-core:54240] [ruby-trunk - Bug #8262] " marcandre (Marc-Andre Lafortune)
@ 2013-04-18 17:10 ` ajack (Jack Weeden)
  2013-04-18 17:22 ` [ruby-core:54435] [ruby-trunk - Feature #8262][Closed] " marcandre (Marc-Andre Lafortune)
  2 siblings, 0 replies; 4+ messages in thread
From: ajack (Jack Weeden) @ 2013-04-18 17:10 UTC (permalink / raw
  To: ruby-core


Issue #8262 has been updated by ajack (Jack Weeden).


OK, this makes perfect sense now! I'd never come across the "-@" syntax for unary operators. Thanks!
----------------------------------------
Feature #8262: Confusing "undefined method" exception when using - operator on a string
https://bugs.ruby-lang.org/issues/8262#change-38712

Author: ajack (Jack Weeden)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 


When trying to use the minus operator (-) on a String, Ruby reports the following exception:

> -"5"
> NoMethodError: undefined method `-@' for "5":String

I'm not sure if this is intended behaviour but it was confusing as it led me to believe I was trying to call `-@"5"` rather than `-"5"` (I realise the minus operator is invalid for a String but I was looking all over for somewhere I'd accidentally typed in an "@" symbol, rather than somewhere I was just trying to call `-"5"`)

Applies to Ruby 1.8.7, 1.9.3 and 2.0.0



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:54435] [ruby-trunk - Feature #8262][Closed] Confusing "undefined method" exception when using - operator on a string
  2013-04-12 16:17 [ruby-core:54234] [ruby-trunk - Bug #8262][Open] Confusing "undefined method" exception when using - operator on a string ajack (Jack Weeden)
  2013-04-12 18:51 ` [ruby-core:54240] [ruby-trunk - Bug #8262] " marcandre (Marc-Andre Lafortune)
  2013-04-18 17:10 ` [ruby-core:54434] [ruby-trunk - Feature " ajack (Jack Weeden)
@ 2013-04-18 17:22 ` marcandre (Marc-Andre Lafortune)
  2 siblings, 0 replies; 4+ messages in thread
From: marcandre (Marc-Andre Lafortune) @ 2013-04-18 17:22 UTC (permalink / raw
  To: ruby-core


Issue #8262 has been updated by marcandre (Marc-Andre Lafortune).

Status changed from Open to Closed

Perfect. I'll close this then, unless someone comes up with a better proposal.
----------------------------------------
Feature #8262: Confusing "undefined method" exception when using - operator on a string
https://bugs.ruby-lang.org/issues/8262#change-38713

Author: ajack (Jack Weeden)
Status: Closed
Priority: Normal
Assignee: 
Category: core
Target version: 


When trying to use the minus operator (-) on a String, Ruby reports the following exception:

> -"5"
> NoMethodError: undefined method `-@' for "5":String

I'm not sure if this is intended behaviour but it was confusing as it led me to believe I was trying to call `-@"5"` rather than `-"5"` (I realise the minus operator is invalid for a String but I was looking all over for somewhere I'd accidentally typed in an "@" symbol, rather than somewhere I was just trying to call `-"5"`)

Applies to Ruby 1.8.7, 1.9.3 and 2.0.0



-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-04-18 17:45 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-12 16:17 [ruby-core:54234] [ruby-trunk - Bug #8262][Open] Confusing "undefined method" exception when using - operator on a string ajack (Jack Weeden)
2013-04-12 18:51 ` [ruby-core:54240] [ruby-trunk - Bug #8262] " marcandre (Marc-Andre Lafortune)
2013-04-18 17:10 ` [ruby-core:54434] [ruby-trunk - Feature " ajack (Jack Weeden)
2013-04-18 17:22 ` [ruby-core:54435] [ruby-trunk - Feature #8262][Closed] " marcandre (Marc-Andre Lafortune)

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).