barsoom / attr_extras

Takes some boilerplate out of Ruby with methods like attr_initialize.
MIT License
560 stars 31 forks source link

How to use super in attr_initialize? #17

Closed bigxiang closed 8 years ago

bigxiang commented 9 years ago

In the document, it says: attr_initialize can also accept a block which will be invoked after initialization. This is useful for calling super appropriately in subclasses or initializing private data as necessary.

But there isn't an example for this case, when I want to call super in attr_initialize, I don't know how to make it working.

class MyBaseClass
  def initialize
    puts 'I am base class'
  end
end

class MyClass < MyBaseClass
  attr_initialize :foo do
    super
  end
end

MyClass.new('foo') # => NoMethodError: super called outside of method

Do I use it incorrectly? I am using Ruby 2.0.0p353

henrik commented 9 years ago

Good catch! I think the README is incorrect here. I'll remove mention of super. AFAIK you can't call super from a block like that. Sorry!

@jferris, any thoughts?

jferris commented 9 years ago

@henrik I thought I had tried this out with super when I submitted the pull request. I'll have to take a look at this on Friday.

henrik commented 9 years ago

@jferris Sounds good, thank you! Maybe some later change broke it.

bigxiang commented 9 years ago

Thank you for looking into this. At first, I wanted to figure it out by myself, but due to my limitation of the knowledge, I couldn't get it :)

henrik commented 9 years ago

@bigxiang Great that you tried, though :) @jferris No worries if you haven't had the time, but any news?

jferris commented 9 years ago

@henrik I haven't gotten to take a look at this yet. I still plan to, though!

henrik commented 8 years ago

Since the docs no longer mention super, I'll close this issue and will consider it an unsupported feature. If someone wants to look into it and add support (if possible), please open a PR!