[#10492] Ruby 1.8.6 preview3 has been released — "Akinori MUSHA" <knu@...>

Hi,

26 messages 2007/03/04
[#10500] Re: Ruby 1.8.6 preview3 has been released — Hugh Sasse <hgs@...> 2007/03/05

On Mon, 5 Mar 2007, Akinori MUSHA wrote:

[#10507] Dynamic Array#join with block — <noreply@...>

Patches item #9055, was opened at 2007-03-05 19:57

12 messages 2007/03/05
[#10520] Re: [ ruby-Patches-9055 ] Dynamic Array#join with block — Nobuyoshi Nakada <nobu@...> 2007/03/06

Hi,

[#10594] grave bug in 1.8.6's thread implementation — Sylvain Joyeux <sylvain.joyeux@...4x.org>

In ext/thread/thread.c, remove_one leaves the list in an inconsistent state.

15 messages 2007/03/14
[#10596] Re: [PATCH] grave bug in 1.8.6's thread implementation — MenTaLguY <mental@...> 2007/03/14

On Thu, 15 Mar 2007 00:15:57 +0900, Sylvain Joyeux <sylvain.joyeux@m4x.org> wrote:

[#10597] Re: [PATCH] grave bug in 1.8.6's thread implementation — Sylvain Joyeux <sylvain.joyeux@...4x.org> 2007/03/14

> > The fix is in thread-mutex-remove_one.diff.

[#10598] Re: [PATCH] grave bug in 1.8.6's thread implementation — MenTaLguY <mental@...> 2007/03/14

On Thu, 15 Mar 2007 01:19:04 +0900, Sylvain Joyeux <sylvain.joyeux@m4x.org> wrote:

[#10599] Re: [PATCH] grave bug in 1.8.6's thread implementation — Sylvain Joyeux <sylvain.joyeux@...4x.org> 2007/03/14

On Wednesday 14 March 2007 17:29, MenTaLguY wrote:

[#10600] Re: [PATCH] grave bug in 1.8.6's thread implementation — MenTaLguY <mental@...> 2007/03/14

On Thu, 15 Mar 2007 01:48:42 +0900, Sylvain Joyeux <sylvain.joyeux@m4x.org> wrote:

[#10615] Multiton in standard library — TRANS <transfire@...>

Hi--

16 messages 2007/03/15
[#10619] Re: Multiton in standard library — Tom Pollard <tomp@...> 2007/03/16

[#10620] Re: Multiton in standard library — TRANS <transfire@...> 2007/03/16

On 3/15/07, Tom Pollard <tomp@earthlink.net> wrote:

[#10646] Marshal.dump shouldn't complain about singletons if the _dump method is defined — <noreply@...>

Bugs item #9376, was opened at 2007-03-19 15:58

12 messages 2007/03/19
[#10647] Re: [ ruby-Bugs-9376 ] Marshal.dump shouldn't complain about singletons if the _dump method is defined — Urabe Shyouhei <shyouhei@...> 2007/03/19

noreply@rubyforge.org wrote:

[#10648] Re: [ ruby-Bugs-9376 ] Marshal.dump shouldn't complain about singletons if the _dump method is defined — Sylvain Joyeux <sylvain.joyeux@...4x.org> 2007/03/19

On Monday 19 March 2007 18:01, Urabe Shyouhei wrote:

[#10651] Re: [ ruby-Bugs-9376 ] Marshal.dump shouldn't complain about singletons if the _dump method is defined — Yukihiro Matsumoto <matz@...> 2007/03/19

Hi,

[#10665] Re: [ ruby-Bugs-9376 ] Marshal.dump shouldn't complain about singletons if the _dump method is defined — "Chris Carter" <cdcarter@...> 2007/03/20

On 3/19/07, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

[#10712] Ruby Method Signatures (was Re: Multiton in standard library) — "Rick DeNatale" <rick.denatale@...>

On 3/19/07, TRANS <transfire@gmail.com> wrote:

10 messages 2007/03/21
[#10715] Re: Ruby Method Signatures (was Re: Multiton in standard library) — Jos Backus <jos@...> 2007/03/22

On 3/19/07, TRANS <transfire@gmail.com> wrote:

[#10798] Virtual classes and 'real' classes -- why? — "John Lam (CLR)" <jflam@...>

I was wondering if someone could help me understand why there's a parallel =

12 messages 2007/03/28
[#10799] Re: Virtual classes and 'real' classes -- why? — MenTaLguY <mental@...> 2007/03/28

On Thu, 29 Mar 2007 04:44:16 +0900, "John Lam (CLR)" <jflam@microsoft.com> wrote:

[PATCH] Join with block

From: "Farrel Lifson" <farrel.lifson@...>
Date: 2007-03-04 11:16:35 UTC
List: ruby-core #10490
This patch adds the ability to give the Array#join method a block like so
 [1,3,2,4].join(' which is X than '){|sep,a,b| a > b ?
sep.sub(/X/,"#{a-b} more") : sep.sub(/X/,"#{b-a} less")}
which produces
 1 which is 2 less than 3 which is 1 more than 2 which is 2 less than 4

The patch is based off the original rb_ary_join and so it handles
nested arrays in the same recursive manner as rb_ary_join:
  [[1,2],[3,4]].join {|sep,a,b| b.class.eql?(Array) ? "->" : ":"}
which produces
  1:2->3:4

The parameters passed into the block are the separator string (sep)
which is the parameter passed to the join method, and the two elements
(a,b) between which sep will be inserted.

The following methods are added/modified:

  * rb_ary_join_m : Calls rb_ary_join or rb_ary_join_block depending
on whether a block is given.
  * rb_ary_join_block: Performs the join using the given block.
  * recursive_join_block: Same functionality as recursive_join but
calls rb_ary_join_block.

Tests are added to sample/test.rb and the definition of
rb_ary_join_block is added to intern.h.

Unfortunately due to some weird transparent proxy errors in South
Africa, I can't log into Rubyforge to add this to the Ruby project
patch tracker. If someone could submit it for me I would appreciate it
(that is if it doesn't get shot out the sky here first)

Thanks,
Farrel

Attachments (1)

join-block-patch.diff (3.26 KB, text/x-diff)
Index: array.c
===================================================================
--- array.c	(revision 11982)
+++ array.c	(working copy)
@@ -1326,6 +1326,70 @@
     return result;
 }
 
+static VALUE
+recursive_join_block(VALUE ary, VALUE argp, int recur)
+{
+  VALUE *arg = (VALUE *)argp;
+  if (recur) {
+	  return rb_str_new2("[...]");
+  }
+  return rb_ary_join_block(arg[0], arg[1]);
+}
+
+VALUE
+rb_ary_join_block(VALUE ary,VALUE sep)
+{
+  if (RARRAY_LEN(ary) == 0) return rb_str_new(0,0);
+  int taint = Qfalse;
+  VALUE result, tmp;
+  if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = Qtrue;
+  result = rb_str_new2("");
+  long i;
+  for (i=0;i<RARRAY_LEN(ary)-1;i++)
+  {
+    tmp = rb_ary_entry(ary,i);
+    switch (TYPE(tmp)) {
+	    case T_STRING:
+	      break;
+	    case T_ARRAY:
+	    {
+		    VALUE args[2];
+		    args[0] = tmp;
+		    args[1] = sep;
+		    tmp = rb_exec_recursive(recursive_join_block, ary, (VALUE)args);
+	    }
+	      break;
+	    default:
+	      tmp = rb_obj_as_string(tmp);
+	  }
+    if (OBJ_TAINTED(tmp)) taint = Qtrue;
+    rb_str_buf_append(result,tmp);
+    VALUE block_args = rb_ary_new3(3,sep,rb_ary_entry(ary,i),rb_ary_entry(ary,i+1));  
+    tmp = rb_obj_as_string(rb_yield(block_args));
+    if (OBJ_TAINTED(tmp)) taint = Qtrue;
+    rb_str_buf_append(result,tmp);
+  }
+  tmp = rb_ary_entry(ary,RARRAY_LEN(ary)-1);
+  switch (TYPE(tmp)) {
+    case T_STRING:
+      break;
+    case T_ARRAY:
+      {
+        VALUE args[2];
+		    args[0] = tmp;
+		    args[1] = sep;
+		    tmp = rb_exec_recursive(recursive_join_block, ary, (VALUE)args);
+	    }
+	      break;
+	    default:
+	      tmp = rb_obj_as_string(tmp);
+	}
+  if (OBJ_TAINTED(tmp)) taint = Qtrue;
+  rb_str_buf_append(result,tmp);
+  if (taint) OBJ_TAINT(result);
+  return result;
+}
+
 /*
  *  call-seq:
  *     array.join(sep=$,)    -> str
@@ -1341,11 +1405,14 @@
 rb_ary_join_m(int argc, VALUE *argv, VALUE ary)
 {
     VALUE sep;
-
+    
     rb_scan_args(argc, argv, "01", &sep);
     if (NIL_P(sep)) sep = rb_output_fs;
     
-    return rb_ary_join(ary, sep);
+    if (rb_block_given_p())
+      return rb_ary_join_block(ary, sep);
+    else
+      return rb_ary_join(ary, sep);
 }
 
 static VALUE
Index: sample/test.rb
===================================================================
--- sample/test.rb	(revision 11982)
+++ sample/test.rb	(working copy)
@@ -748,6 +748,18 @@
 $x.concat($x)
 test_ok($x == [1,2,3,1,2,3])
 
+# join
+$x = [1,3,2,4]
+test_ok("1324" == $x.join)
+test_ok("1-3-2-4" == $x.join("-"))
+test_ok("1324" == $x.join(){})
+test_ok("1324" == $x.join("-"){})
+test_ok("1-3-2-4" == $x.join{"-"})
+test_ok("1<3>2<4" == $x.join{|sep,a,b|a > b ? ">" : "<"})
+test_ok("1-<3->2-<4" == $x.join("-"){|sep,a,b| sep + (a > b ? ">" : "<")})
+$x = [[1,2],[3,4]]
+test_ok("1:2->3:4"==$x.join{|sep,a,b| b.class.eql?(Array) ? "->" : ":"})
+
 test_check "hash"
 $x = {1=>2, 2=>4, 3=>6}
 $y = {1, 2, 2, 4, 3, 6}
Index: intern.h
===================================================================
--- intern.h	(revision 11982)
+++ intern.h	(working copy)
@@ -55,6 +55,7 @@
 VALUE rb_ary_entry(VALUE, long);
 VALUE rb_ary_each(VALUE);
 VALUE rb_ary_join(VALUE, VALUE);
+VALUE rb_ary_join_block(VALUE, VALUE);
 VALUE rb_ary_print_on(VALUE, VALUE);
 VALUE rb_ary_reverse(VALUE);
 VALUE rb_ary_sort(VALUE);

In This Thread

Prev Next