Forum: Ruby-core [ruby-trunk - Bug #7782][Open] Struct both has and does not have an allocator

Posted by Thomas Sawyer (7rans)
on 2013-02-04 17:33
(Received via mailing list)
Issue #7782 has been reported by trans (Thomas Sawyer).

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

Author: trans (Thomas Sawyer)
Status: Open
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
Posted by matz (Yukihiro Matsumoto) (Guest)
on 2013-02-05 01:47
(Received via mailing list)
Issue #7782 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Closed

"allocator" in the error message does not mean #allocate method, but 
internal C function (thus TypeError not NoMethodError).
We haven't provide the way to check if a class can be allocated, except 
for actually allocating an object.
I don't think we need to prepare the way to check explicitly.

Matz.

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

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
Posted by Thomas Sawyer (7rans)
on 2013-02-05 02:59
(Received via mailing list)
Issue #7782 has been updated by trans (Thomas Sawyer).


So you think rescuing the error is good enough. Ok, I'll handle it that 
way. Would it be prudent to make the error very specific, e.g. 
`UndefinedAllocatorError`. So that rescue clauses can be sure to catch 
that specifically?

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

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
Posted by matz (Yukihiro Matsumoto) (Guest)
on 2013-02-05 03:14
(Received via mailing list)
Issue #7782 has been updated by matz (Yukihiro Matsumoto).


I am not positive about adding exception classes.  So far, TypeError 
only caused by inexistence of allocator.

Matz.

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

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
Posted by Thomas Sawyer (7rans)
on 2013-02-05 19:37
(Received via mailing list)
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
Posted by drbrain (Eric Hodel) (Guest)
on 2013-02-05 20:39
(Received via mailing list)
Issue #7782 has been updated by drbrain (Eric Hodel).


Sounds like you are reinventing begin; rescue; else; end: 
https://github.com/ruby/ruby/blob/trunk/doc/syntax...
----------------------------------------
Bug #7782: Struct both has and does not have an allocator
https://bugs.ruby-lang.org/issues/7782#change-35867

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
Posted by Thomas Sawyer (7rans)
on 2013-02-05 21:40
(Received via mailing list)
Issue #7782 has been updated by trans (Thomas Sawyer).


Ah, `else`. I've never used that, and forgotten about it.

So I should be able to write instead:

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

Reads kind of funny. But okay. I will try that. Thanks.

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

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
Posted by Thomas Sawyer (7rans)
on 2013-02-05 22:27
(Received via mailing list)
Issue #7782 has been updated by trans (Thomas Sawyer).


Ok. One last comment on this:

> I am not positive about adding exception classes. So far, TypeError only caused 
by inexistence of allocator.

Assessments like "So far", are what make's a coder worry. In future that 
could change, then my code suddenly has potential error in it. However, 
I realize this is very unlikely, so I won't fret over it. But unlikely 
or no, I'd rather have zero probability of worry!

P.S. @drbrain It worked. Thanks again.
----------------------------------------
Bug #7782: Struct both has and does not have an allocator
https://bugs.ruby-lang.org/issues/7782#change-35873

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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.