Issue #7782 has been updated by trans (Thomas Sawyer).


=begin
But is not so nice to have to write code like this:

    success = begin
      object = type.allocate
      true
    rescue TypeError
      false
    end

    if success
      ...
    else
      ...
    end

Instead of like:

    begin
      object = type.allocate
      ...
    rescue TypeError
      ...
    end

Can't do it b/c what if first `...` code causes different TypeError?

Actually the more I think about it the more I am inclined to add a core extension, maybe:

    def try_allocate
      begin
        allocate
      rescue TypeError
        nil
      end
    end

Then we could write:

    if object = try_allocate
      ...
    else
      ...
    end

=end
----------------------------------------
Bug #7782: Struct both has and does not have an allocator
https://bugs.ruby-lang.org/issues/7782#change-35866

Author: trans (Thomas Sawyer)
Status: Closed
Priority: Normal
Assignee: 
Category: core
Target version: 1.9.3
ruby -v: ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux]


=begin
One the one hand:

    >> Struct.allocate
    TypeError: allocator undefined for Struct
        from (irb):1:in `allocate'
        from (irb):1
        from /opt/Ruby/1.9.3-p327/bin/irb:12:in `<main>'

But on the other:

    >> Struct.method(:allocate)
    => #<Method: Class#allocate>

In my current case, I need a reliable way to check if a class can be allocated or not. How can one do this if the method is remains present even when it can not be used?
=end



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