Project

General

Profile

Actions

Bug #20647

closed

Invalid return in class/module body from within singleton class

Added by kddnewton (Kevin Newton) 10 months ago. Updated 10 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:118670]

Description

Currently, Ruby will raise a syntax error for invalid returns within class and module bodies, but skips it for singleton classes. I think we should be able to add this same error for singleton classes, as I believe these always raise a LocalJumpError.

class << Object
  return
end

Updated by zverok (Victor Shepelev) 10 months ago

That’s actually interesting, because return in the middle of the class body, while might be considered questionable, is also meaningful.

I think making it a syntax error in normal classes was (probably?) a decision from before 2.4 (when top-level return had no useful semantics).

I can imagine code like

class MyService
  # some generic stuff

  return if RUBY_PLATFORM.match?(/windows/i)

  # some Unix-only stuff
end

...and even more so, the same in the middle of a singleton class definition in some DSL method :shrug:

Actions #3

Updated by nobu (Nobuyoshi Nakada) 10 months ago

  • Status changed from Open to Closed

Applied in changeset git|e642ddf7ae86e306674559ae745823fdbf56ea10.


[Bug #20647] Disallow return directly within a singleton class

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0