Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

attr_accesor_with_default alias reader method

#6099
attr_accessor_with_default :something_processed, false
alias_method :something_processed?, :something_processed

seems to be a problem because the reader looks like it is redefined on line 26 in "attr_accessor_with_default.rb"

class << self; attr_reader :#{sym} end  #   class << self; attr_reader :age end
x = Blubb.new
x.something_processed        # => false
x.something_processed?       # => false
x.something_processed = true # => true
x.something_processed        # => true
x.something_processed?       # => false

imho the last line should also return true

i'm sorry if that behavior is how it's intended

Reported by bhu · December 2nd, 2010 @ 02:54 AM

State: open
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Aditya Sanghi
    Aditya Sanghi
    • Importance changed from to Low

    @bhu, it is indeed odd and I can reproduce this behavior.

    I don't think it was not how it was intended but unfortunately this is how the functionality got implemented.
    The writer redefines the reader to clear out the default.

    I wish it was written a bit more cleanly such that reader and writers were only defined once.

    something like (pseudocode)

      def sym=(value)
        @sym_marked = true
        @sym = value
      end
      def sym
        @sym_marked ? @sym : default_value # default_value would return the default value or invoke the block
      end
    

    Feel free to implement and patch.

    December 2nd, 2010 @ 08:31 AM

  2. bhu
    bhu

    Hmm i can't upload the diff file. Am i doing something wrong?

    December 12th, 2010 @ 02:27 PM

  3. bhu
    bhu

    tried patch tags, state:resolved, patch prefix, patch/diff file extensions.

    I used the sending_patches guide.

    There is no error message. Every time I try to attach a file, nothing happens after submitting.
    Sorry for making it unnecessary complicated.

    December 12th, 2010 @ 05:07 PM

  4. Aditya Sanghi
    Aditya Sanghi

    Try putting it in a gist. I'll try to upload the patch here. Sorry for the delay.

    December 20th, 2010 @ 07:14 PM

  5. bhu
    bhu

    Here my latest version in case of tagging.
    https://gist.github.com/748873
    Hopefully you can tell me what i did wrong.

    I hope you like the solution with the additional method.

    If there are any problems please inform me. First ticket so i don't really know if i did everything right.

    The "(@sym_marked ||= false)" is just to get rid of uninitialized variable warning.

    December 20th, 2010 @ 07:43 PM

  6. Aditya Sanghi
  7. Aditya Sanghi
    Aditya Sanghi
    • State changed from new to open
    • Tag set to patch

    December 20th, 2010 @ 09:24 PM

  8. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    March 21st, 2011 @ 12:00 AM

  9. rails
    rails
    • State changed from open to stale

    March 21st, 2011 @ 12:00 AM

  10. bhu
    bhu
    • State changed from stale to open

    The in my eyes error still exists in 3.0.5. The diff is also included with the tests. The ticket just needs to be reviewed, or did i miss something? I think this could be an cleaner an better useable solution than the old version. [state:open]

    March 21st, 2011 @ 09:23 AM