[#36711] [Ruby 1.9 - Bug #4821][Open] Random Segfaults (in start_thread?) — Ivan Bortko <b2630639@...>

22 messages 2011/06/03

[#36730] [Ruby 1.9 - Feature #4824][Open] Provide method Kernel#executed? — Lazaridis Ilias <ilias@...>

56 messages 2011/06/04

[#36750] [Ruby 1.9 - Feature #4830][Open] Provide Default Variables for Array#each and other iterators — Lazaridis Ilias <ilias@...>

24 messages 2011/06/05

[#36785] [Ruby 1.9 - Feature #4840][Open] Allow returning from require — Rodrigo Rosenfeld Rosas <rr.rosas@...>

53 messages 2011/06/06
[#36811] Re: [Ruby 1.9 - Feature #4840][Open] Allow returning from require — Yusuke ENDOH <mame@...> 2011/06/07

Hello,

[#36799] [Ruby 1.9 - Feature #4845][Open] Provide Class#cb_object_instantiated_from_literal(object) — Lazaridis Ilias <ilias@...>

11 messages 2011/06/06

[#36834] [Ruby 1.9 - Feature #3905] rb_clear_cache_by_class() called often during GC for non-blocking I/O — Charles Nutter <headius@...>

10 messages 2011/06/08
[#36860] Re: [Ruby 1.9 - Feature #3905] rb_clear_cache_by_class() called often during GC for non-blocking I/O — Eric Wong <normalperson@...> 2011/06/08

Charles Nutter <headius@headius.com> wrote:

[#36863] Object#trust vs Object#taint — Aaron Patterson <aaron@...>

Hi,

16 messages 2011/06/08
[#36866] Re: Object#trust vs Object#taint — Yukihiro Matsumoto <matz@...> 2011/06/08

Hi,

[#36873] Re: Object#trust vs Object#taint — Aaron Patterson <aaron@...> 2011/06/09

On Thu, Jun 09, 2011 at 07:49:06AM +0900, Yukihiro Matsumoto wrote:

[#37071] [Ruby 1.9 - Feature #4877][Open] Unify Variable Expansion within Strings — Lazaridis Ilias <ilias@...>

12 messages 2011/06/12

[#37106] ruby core tutorials location — Roger Pack <rogerdpack2@...>

Hello all.

10 messages 2011/06/13
[#37107] Re: ruby core tutorials location — Jon <jon.forums@...> 2011/06/13

> Hello all.

[#37115] Re: ruby core tutorials location — Roger Pack <rogerdpack2@...> 2011/06/13

> Rather than adding links to source code, I would prefer the wikibooks link and others under a new Tutorials section of http://www.ruby-lang.org/en/documentation/ as well as adding http://ruby.runpaint.org/ to the existing Getting Started section.

[#37117] Re: ruby core tutorials location — Jon <jon.forums@...> 2011/06/13

> > Rather than adding links to source code, I would prefer the wikibooks link and others under a new Tutorials section of http://www.ruby-lang.org/en/documentation/ as well as adding http://ruby.runpaint.org/ to the existing Getting Started section.

[#37128] Re: ruby core tutorials location — Roger Pack <rogerdpack2@...> 2011/06/14

> I like what you're trying to do and see how great that tutorial connection from rdoc/yard could be, say, mixing with existing ruby-doc.org and rubydoc.info. ut I question embedding source links to info in which the info can easily grow outdated or abandoned as time passes. I also question the ongoing maintenance burdens.

[#37137] Re: ruby core tutorials location — Jon <jon.forums@...> 2011/06/14

> > I like what you're trying to do and see how great that tutorial connection from rdoc/yard could be, say, mixing with existing ruby-doc.org and rubydoc.info. ut I question embedding source links to info in which the info can easily grow outdated or abandoned as time passes. I also question the ongoing maintenance burdens.

[#37164] [Ruby 1.9 - Feature #4890][Open] Enumerable#lazy — Yutaka HARA <redmine@...>

30 messages 2011/06/16

[#37170] [Ruby 1.9 - Bug #4893][Open] Literal Instantiation breaks Object Model — Lazaridis Ilias <ilias@...>

61 messages 2011/06/16

[#37207] [Ruby 1.9 - Feature #4897][Open] Define Math::TAU and BigMath.TAU. The "true" circle constant, Tau=2*Pi. See http://tauday.com/ — Simon Baird <simon.baird@...>

43 messages 2011/06/17

[#37286] [Ruby 1.9 - Bug #4916][Open] [BUG] Segmentation fault - dyld: lazy symbol binding failed: Symbol not found: _ASN1_put_eoc — Hiroshi NAKAMURA <nakahiro@...>

9 messages 2011/06/22

[#37324] [Ruby 1.9 - Bug #4923][Open] [ext/openssl] test_ssl.rb: test_client_auth fails — Martin Bosslet <Martin.Bosslet@...>

19 messages 2011/06/23

[#37576] [Ruby 1.9 - Feature #4938][Open] Add Random.bytes [patch] — Marc-Andre Lafortune <ruby-core@...>

13 messages 2011/06/27

[#37612] [Ruby 1.9 - Bug #4941][Open] cannot load such file -- rubygems.rb (LoadError) — Lazaridis Ilias <ilias@...>

25 messages 2011/06/28

[ruby-core:37248] [Ruby 1.9 - Feature #4910] Classes as factories

From: Robert Klemme <shortcutter@...>
Date: 2011-06-20 14:29:42 UTC
List: ruby-core #37248
Issue #4910 has been updated by Robert Klemme.


Benoit Daloze wrote:
> Hello,
> 
> Robert Klemme wrote:
> > I suggest to add these two to class Class:
> > 
> > class Class
> >   alias call new
> > 
> >   def to_proc(*args)
> >     lambda {|*a| new(*args)}
> >   end
> > end
> 
> Did you want to mean:
> def to_proc
>   lambda { |*args| new(*args) } # or maybe lambda { |args| new(*args) }
> end
> ?
> 
> #to_proc is called with no arguments (Symbol.instance_method(:to_proc).arity # => 0).

No, it was meant exactly as stated.  Advantage is that you can provide parameters to #new if needed while mapping the parameterless call of to_proc easily to the parameterless call of Class#new.

> > Then we can use class instances where blocks are needed and can easily use them as factory instances using the general contract of #call (see example attached).
> 
> I don't really see the advantage of defining #call, you could use #new instead at line 16.
> If you want more flexibility, I believe it is fine to use a block.

That's the exact point: by aliasing #new to #call we can pass in a lambda OR a class instance.  The most general contract would then be '#call'able (i.e. an anonymous callback function) and as a shortcut we can pass in a class instance.

> But I like Class#to_proc, and it is indeed some kind of factory helper:
> 
>   Pos = Struct.new :x,:y
>   [[1,2],[3,4]].map(&Pos) # => [#&lt;struct Pos x=1, y=2&gt;, #&lt;struct Pos x=3, y=4&gt;]
>   # instead of
>   [[1,2],[3,4]].map { |x,y| Pos.new(x,y) }
> 
>   # note neither #to_proc defined as "lambda { |*args| new(*args) }" nor map(&Pos.method(:new)) would work:
>   # ([#&lt;struct Pos x=[1, 2], y=nil&gt;,...])
> 
> The obvious limitation being the lack of flexibility for common arguments (e.g.: y always the same). You would then have to use an explicit block.
> 
> I do not know if it is worth to add it for this specific case, but it can be nice.

I had considered that case as well and felt it might not be as common as the case where we try to provide arguments.  I do not have any statistics though and I hope for others shedding some more light what they deem more useful.

A variant would be

class Class
  def to_proc(*args)
    if args.empty?
      lambda {|*a| new(*a)}
    else
      lambda {|*a| new(*args)}
    end
  end
end

In other words: if arguments are passed to to_proc use them as sole method arguments for #new; if not, use whatever is passed to the proc (which would support your mapping example).

We could probably make things even more complex by appending *a to *args and truncating the list with the arity of #new at the time of invocation of the block (or, more efficient, time of call of to_proc).

> I am also unsure if we need factories in Ruby (certainly not like in statically typed languages).

Any class in Ruby *is* a factory object already with method #new being the factory method.
----------------------------------------
Feature #4910: Classes as factories
http://redmine.ruby-lang.org/issues/4910

Author: Robert Klemme
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


I suggest to add these two to class Class:

class Class
  alias call new

  def to_proc(*args)
    lambda {|*a| new(*args)}
  end
end

Then we can use class instances where blocks are needed and can easily use them as factory instances using the general contract of #call (see example attached).


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

In This Thread