New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module#parent_name broken for frozen classes #27637

Closed
ccutrer opened this Issue Jan 10, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@ccutrer
Contributor

ccutrer commented Jan 10, 2017

Steps to reproduce

class A
  class B
  end
end

A::B.freeze
A::B.parent_name

Expected behavior

Should return "A", and not try to cache the parent_name into an instance variable on the frozen class

Actual behavior

RuntimeError: can't modify frozen #<Class:A::B>

System configuration

Rails version: 4.2.7.1 (though the code hasn't changed in 5.0 or master)
Ruby version: 2.3.1

@maclover7

This comment has been minimized.

Member

maclover7 commented Jan 11, 2017

Caching introduced many moons ago, via 269c6c6.

@coreyward

This comment has been minimized.

Contributor

coreyward commented Jan 13, 2017

What do you think about checking for frozen? before defining?

Current:

def parent_name
  unless defined? @parent_name
    @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
  end
  @parent_name
end

Proposed:

def parent_name
  return @parent_name if defined?(@parent_name)
  parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
  @parent_name = parent_name unless frozen?
  parent_name
end
@ccutrer

This comment has been minimized.

Contributor

ccutrer commented Jan 13, 2017

👍 from me

@maclover7

This comment has been minimized.

Member

maclover7 commented Jan 15, 2017

@coreyward Can you please open up a PR with that code change and a regression test? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment