[#89088] More questions about =~ — GGarramuno@... (GGarramuno)

irb(main):006:1* class String

14 messages 2004/01/01

[#89119] Loop/Iterator questions — GGarramuno@... (GGarramuno)

1) Is there anything like Perl's continue block available? This is

15 messages 2004/01/02

[#89189] Best way to send mail in ruby — Bauduin Raphael <rb@...>

Hi,

11 messages 2004/01/03

[#89193] Simple Ruby DB apps/programs ... — Useko Netsumi <usenets@...>

I was wondering if there are some example of small Ruby(1.8.1) Database

14 messages 2004/01/03

[#89261] class Time doesn't pass year 2038? — Jean-Baptiste <temuphaey0@...>

15 messages 2004/01/05

[#89339] Compression (besides Huffman) and Ruby — "Josef 'Jupp' SCHUGT" <jupp@...>

Hi!

14 messages 2004/01/07

[#89367] Database applications and OOness — Tim Bates <tim@...>

People,

63 messages 2004/01/07
[#89455] Re: Database applications and OOness — "dhtapp" <dhtapp@...> 2004/01/08

I've been watching this thread with a great deal of interest. I'm

[#89456] block delimiting — Pete Yadlowsky <pmy@...> 2004/01/08

[#89465] Re: block delimiting — Austin Ziegler <austin@...> 2004/01/08

On Fri, 9 Jan 2004 04:33:15 +0900, Pete y wrote:

[#89453] ruby 1.8.1 windows installer — KONTRA Gergely <kgergely@...>

Hi!

26 messages 2004/01/08
[#89716] Re: ruby 1.8.1 windows installer — intc_ctor@... (Phil Tomson) 2004/01/12

>

[#89860] Re: ruby 1.8.1 windows installer — Alan Davies <NOSPAMcs96and@...> 2004/01/14

> Since the first edition of the Pickaxe book didn't exactly fly off the

[#89460] Re: block delimiting — "Mike Wilson" <wmwilson01@...>

21 messages 2004/01/08

[#89590] regex to NOT match? — Ruby Baby <ruby@...>

Sorry it seems like the smallest thing, but I'm stuck on this.

16 messages 2004/01/10

[#89611] Converting a string to an array of tokens — "John W. Long" <ws@...>

Is there a fast way to convert a string into a list of tokens?

17 messages 2004/01/11

[#89672] faster integer arithmetics & arbitrary precision floating number — David Garamond <lists@...6.isreserved.com>

1. Is there a way in Ruby to speed up 32bit integer arithmetics (only

43 messages 2004/01/12
[#89686] Re: faster integer arithmetics & arbitrary precision floating number — Ara.T.Howard@... 2004/01/12

On Tue, 13 Jan 2004, David Garamond wrote:

[#89709] Re: faster integer arithmetics & arbitrary precision floating number — Charles Mills <boson@...> 2004/01/12

What abouts Rubys design would make integer arithmetic slower than integer

[#89710] Re: faster integer arithmetics & arbitrary precision floating number — Dave Thomas <dave@...> 2004/01/12

[#89711] Re: faster integer arithmetics & arbitrary precision floating number — Charles Mills <boson@...> 2004/01/12

On Tue, 13 Jan 2004, Dave Thomas wrote:

[#89718] Getting the tail of a list? — Carsten Eckelmann <careck@...42.com>

Hi everybody,

19 messages 2004/01/12

[#89796] Ruby OS mentioned on /. — intc_ctor@... (Phil Tomson)

http://developers.slashdot.org/developers/04/01/13/0123250.shtml?tid=185&tid=190

20 messages 2004/01/13
[#89805] Re: Ruby OS mentioned on /. — Paul William <maillist@...> 2004/01/13

./ normally does not have vaporware... are a bunch of ruby (a very high

[#89806] Re: Ruby OS mentioned on /. — "Zach Dennis" <zdennis@...> 2004/01/13

Somehow i have this strange feeling that not all ruby peeps are strictly

[#89975] drb, firewall, ssh tunneling, and yield — Joel VanderWerf <vjoel@...>

14 messages 2004/01/16
[#89976] Re: drb, firewall, ssh tunneling, and yield — Nathaniel Talbott <nathaniel@...> 2004/01/16

On Jan 15, 2004, at 19:10, Joel VanderWerf wrote:

[#90013] Fighting Ruby's bad fame — gabriele renzi <surrender_it@...1.vip.ukl.yahoo.com>

Hi gurus and nubys,

42 messages 2004/01/16
[#90097] Re: Fighting Ruby's bad fame — ptkwt@... (Phil Tomson) 2004/01/18

In article <af53b0ba.0401171921.7cf9b9b7@posting.google.com>,

[#90023] Installing a program Unix-like — Malte Milatz <malteDELETETHIS@...>

Users of Linux, FreeBSD etc. are used to downloading an archive,

13 messages 2004/01/16

[#90077] long expression syntax — rick.hu@... (Rick Hu)

why do I get a syntax error for

13 messages 2004/01/17

[#90086] is Ruby the right language for these projects? — Ruby Baby <ruby@...>

Please forgive my self-centered question. I've been learning all about Ruby

16 messages 2004/01/18

[#90139] segfaults on mandrake... — Ferenc Engard <ferenc@...>

Hello,

16 messages 2004/01/18

[#90200] regex help — Chris Morris <chrismo@...>

I need a re such that:

18 messages 2004/01/19

[#90228] Re: New to Python: my impression v. Perl/Ruby — ptkwt@... (Phil Tomson)

In article <mailman.493.1074484056.12720.python-list@python.org>,

36 messages 2004/01/20
[#90292] Re: New to Python: my impression v. Perl/Ruby — Ville Vainio <ville.spamstermeister.vainio@...> 2004/01/20

>>>>> "Phil" == Phil Tomson <ptkwt@aracnet.com> writes:

[#90294] Re: New to Python: my impression v. Perl/Ruby — "Zach Dennis" <zdennis@...> 2004/01/20

Ville>Though "sending messages" to int literals is a syntax error.

[#90332] Re: New to Python: my impression v. Perl/Ruby — GGarramuno@... (GGarramuno) 2004/01/21

"Zach Dennis" <zdennis@mktec.com> wrote in message news:<AKEKIKLMCFIHPEAHKAAICEOHHFAA.zdennis@mktec.com>...

[#90333] Re: New to Python: my impression v. Perl/Ruby — Gregory Millam <walker@...> 2004/01/21

Received: Wed, 21 Jan 2004 16:59:59 +0900

[#90317] Re: Proposal for programming language of the year — "Volkmann, Mark" <Mark.Volkmann@...>

I think one of the main points of learning a new language each year is that

18 messages 2004/01/21

[#90354] Modules as namespace — gm@... (George Moschovitis)

Hello everyone,

16 messages 2004/01/21

[#90405] Very basic Ruby docs/books/tutorial? — Robert Feldt <feldt@...>

Hello,

12 messages 2004/01/22

[#90472] Ruby/Extensions v0.3 released — Gavin Sinclair <gsinclair@...>

A new version of Ruby/Extensions, a suite of useful methods added to

17 messages 2004/01/23

[#90505] Why is to_a going to be obsolete? — Patrick Bennett <patrick.bennett@...>

I find it immensely useful when dealing with arrays to be able to

25 messages 2004/01/23
[#90507] Re: Why is to_a going to be obsolete? — Gennady <gfb@...> 2004/01/23

Patrick Bennett wrote:

[#90510] Re: Why is to_a going to be obsolete? — Patrick Bennett <patrick.bennett@...> 2004/01/23

Hmmm, thanks, but it's a bit 'non-obvious' to casual Ruby programmers

[#90512] Re: Why is to_a going to be obsolete? — Gennady <gfb@...> 2004/01/23

[#90524] Re: Why is to_a going to be obsolete? — "T. Onoma" <transami@...> 2004/01/23

On Friday 23 January 2004 06:43 pm, Gennady wrote:

[#90598] perl bug File::Basename and Perl's nature — xah@... (Xah Lee)

Just bumped into another irresponsibility in perl.

19 messages 2004/01/25

[#90667] ruby-math and "why is ** not abelian?" — vanjac12@... (Van Jacques)

I was reading the 1st thread in the ruby-math discussion at

11 messages 2004/01/26

[#90750] choosing ruby? — Piergiuliano Bossi <p_bossi_AGAINST_SPAM@...>

We are on the way to start a new project, a web application with a bunch

20 messages 2004/01/27

[#90756] Editor — Safran von Twesla <me@...>

Hi,

20 messages 2004/01/27

[#90770] newbee question about "missing" hash methods +, += and << — benny <linux@...>

Hi,

25 messages 2004/01/27

[#90913] vimrc for Ruby or rubytidy — Theodore Knab <tjk@...>

Does someone have a '.vimrc' file they will share

17 messages 2004/01/29
[#90914] Re: vimrc for Ruby or rubytidy — "Gavin Sinclair" <gsinclair@...> 2004/01/29

> Does someone have a '.vimrc' file they will share

[#90971] time comparison — tony summerfelt <snowzone5@...>

i want to parse and trim a log file. the date format log file looks like:

13 messages 2004/01/29

[#91005] Ruby and Perl Integration — "John W. Long" <ws@...>

All this talk about RJNI has gotten me thinking. Has anyone attempted to

17 messages 2004/01/30
[#91007] Re: Ruby and Perl Integration — Thomas Adam <thomas_adam16@...> 2004/01/30

--- "John W. Long" <ws@johnwlong.com> wrote:

[#91056] principle of most suprise — tony summerfelt <snowzone5@...>

gah, ruby is doing it to me again:

31 messages 2004/01/30

[#91071] Accesing to private attributes — "Imobach =?iso-8859-15?q?Gonz=E1lez_Sosa?=" <imodev@...>

Hi all,

14 messages 2004/01/30

[#91088] flip flop operator and assignment — ptkwt@... (Phil Tomson)

I'm working on the pattern matching section for

25 messages 2004/01/31

[#91089] No difference between .. and ... flip/flop operators? — ptkwt@... (Phil Tomson)

50 messages 2004/01/31

[#91099] Ruby 1.8.1 REXML performance — Steven Jenkins <steven.jenkins@...>

I have a script that uses REXML to stream parse an XML file and load a

27 messages 2004/01/31

[#91104] graphics lib? — Alwin Blok <alwinblok@...>

Hello,

38 messages 2004/01/31
[#91262] Re: graphics lib? — Simon Strandgaard <neoneye@...> 2004/02/02

On Sun, 01 Feb 2004 16:18:50 -0600, Charles Comstock wrote:

[#91362] Re: graphics lib? — Charles Comstock <cc1@...> 2004/02/03

Simon Strandgaard wrote:

Re: Nested scopes and the Singleton pattern

From: nobu.nokada@...
Date: 2004-01-16 03:20:59 UTC
List: ruby-talk #89989
Hi,

At Fri, 16 Jan 2004 07:46:39 +0900,
Christoph wrote:
> > > > Just using Thread.critical.  And seems close to your idea
> > > > excepting for it uses "instance" method rather than "new".
> >
> > Not accurate, it uses 3-state; before, during and after
> > creation, but I feel it should use mutex or something.
> 
> You probably always need a 3-state if you want a self
> modifying first instance call (at least in some implicit way).
> 
> Here is a mutexy version of singleton.rb I wrote some time
> ago - it is probably more robust then the current code but equally
> obscure - sorry ...

Due to the line ending codes, your patch contains whole files.
Following is a bit modified version.


Index: lib/singleton.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/singleton.rb,v
retrieving revision 1.21
diff -u -2 -p -d -r1.21 singleton.rb
--- lib/singleton.rb	22 Aug 2003 08:09:58 -0000	1.21
+++ lib/singleton.rb	16 Jan 2004 01:26:06 -0000
@@ -12,5 +12,5 @@
 #    a,b  = Klass.instance, Klass.instance
 #    a == b   # => true
-#    a.new    #  NoMethodError - new is private ...
+#    Klass.new    #  NoMethodError - new is private ...
 #
 # *  ``The instance'' is created at instanciation time, in other
@@ -31,7 +31,5 @@
 #
 # Providing (or modifying) the class methods
-# *  Klass.inherited(sub_klass) and Klass.clone()  - 
-#    to ensure that the Singleton pattern is properly
-#    inherited and cloned.
+# *  Klass.inherited(sub_klass) and Klass.initialize_copy()
 #
 # *  Klass.instance()  -  returning ``the instance''. After a
@@ -45,20 +43,12 @@
 # *  Klass._load(str)  -  calling Klass.instance()
 #
-# *  Klass._instanciate?()  -  returning ``the instance'' or
-#    nil. This hook method puts a second (or nth) thread calling
-#    Klass.instance() on a waiting loop. The return value
-#    signifies the successful completion or premature termination
-#    of the first, or more generally, current "instanciation thread".
-#
 #
 # The instance method of Singleton are
-# * clone and dup - raising TypeErrors to prevent cloning or duping
+# * clone and dup - raising TypeErrors to prevent cloning
 #
-# *  _dump(depth) - returning the empty string.  Marshalling strips
-#    by default all state information, e.g. instance variables and
-#    taint state, from ``the instance''.  Providing custom _load(str)
-#    and _dump(depth) hooks allows the (partially) resurrections of
-#    a previous state of ``the instance''.
-
+# *  _dump(depth) - returning the empty string, in other words
+#    marshalling strips all state information. Providing custom
+#    _load(str) and _dump(depth) hooks allows the (partially) 
+#    resurrections of a previous state of ``the instance''.
 
 
@@ -82,60 +72,61 @@ end
 class << Singleton
   #  Method body of first instance call.
-  FirstInstanceCall = proc do
-    #  @__instance__ takes on one of the following values
-    #  * nil     -  before and after a failed creation
-    #  * false  -  during creation
-    #  * sub_class instance  -  after a successful creation
-    #  the form makes up for the lack of returns in progs
-    Thread.critical = true
-    if  @__instance__.nil?
-      @__instance__  = false
-      Thread.critical = false
-      begin
+  FirstInstanceCall = proc do ||
+    critical, Thread.critical = Thread.critical, true
+    begin
+      if instanciating = @__instance__.nil?
+        @__instance__ = false
+        Thread.critical = critical
         @__instance__ = new
-      ensure
-        if @__instance__
-          class <<self
-            remove_method :instance
-            def instance; @__instance__ end
-          end
-        else
-          @__instance__ = nil #  failed instance creation
-        end
+      elsif !@__instance__
+        @__instanciating_queue__ << Thread.current
+        Thread.stop
+        Thread.critical = true
+        retry
       end
-    elsif  _instanciate?()
-      Thread.critical = false    
-    else
-      @__instance__  = false
-      Thread.critical = false
-      begin
-        @__instance__ = new
-      ensure
+      @__instance__
+    ensure
+      if instanciating
+        Thread.critical= true
         if @__instance__
-          class <<self
+          class << self
             remove_method :instance
             def instance; @__instance__ end
           end
+          remove_instance_variable(@__instanciating_queue__).each do |thr|
+            thr.wakeup
+          end
         else
           @__instance__ = nil
+          if thr = @__instanciating_queue__.shift
+            thr.wakeup
+          end
         end
       end
+      Thread.critical = critical
     end
-    @__instance__
   end
   
-  module SingletonClassMethods  
-    # properly clone the Singleton pattern - did you know
-    # that duping doesn't copy class methods?  
-    def clone
-      Singleton.__init__(super)
+  module SingletonClassMethods
+    def self.extended(klass)
+      klass.instance_eval { 
+        @__instance__ = nil
+        @__instanciating_queue__ = [] 
+      }
+      class << klass
+        define_method(:instance, FirstInstanceCall)
+      end
     end
     
     private
     
-    #  ensure that the Singleton pattern is properly inherited   
+    def initialize_copy(orig)
+      super
+      SingletonClassMethods.extended(self)
+    end
+      
     def inherited(sub_klass)
       super
-      Singleton.__init__(sub_klass)
+      SingletonClassMethods.extended(sub_klass)
     end
     
@@ -143,23 +134,6 @@ class << Singleton
       instance 
     end
-    
-    # waiting-loop hook
-    def _instanciate?()
-      while false.equal?(@__instance__)
-        Thread.critical = false
-        sleep(0.08)   # timeout
-        Thread.critical = true
-      end
-      @__instance__
-    end
   end
   
-  def __init__(klass)
-    klass.instance_eval { @__instance__ = nil }
-    class << klass
-      define_method(:instance,FirstInstanceCall)
-    end
-    klass
-  end
   
   private
@@ -177,7 +151,7 @@ class << Singleton
   def included(klass)
     super
-    klass.private_class_method  :new, :allocate
+    klass.private_class_method :new,:allocate
     klass.extend SingletonClassMethods
-    Singleton.__init__(klass)
+    SingletonClassMethods.extended(klass)
   end
 end
@@ -186,9 +160,4 @@ end
 
 if __FILE__ == $0
-
-def num_of_instances(klass)
-    "#{ObjectSpace.each_object(klass){}} #{klass} instance(s)"
-end 
-
 # The basic and most important example.
 
@@ -196,5 +165,7 @@ class SomeSingletonClass
   include Singleton
 end
-puts "There are #{num_of_instances(SomeSingletonClass)}" 
+
+num =  ObjectSpace.each_object(SomeSingletonClass) {}
+puts "There are #{num} of SomeSingletonClass instances" 
 
 a = SomeSingletonClass.instance
@@ -209,90 +180,81 @@ end
 
 
+puts "\nThreaded example with exception"
+$stdout.sync= true
 
-puts "\nThreaded example with exception and customized #_instanciate?() hook"; p
-Thread.abort_on_exception = false
+class Foo < SomeSingletonClass
+  @attempts = 0
 
-class Ups < SomeSingletonClass
   def initialize
-    self.class.__sleep
-    puts "initialize called by thread ##{Thread.current[:i]}"
-  end
-end
-  
-class << Ups
-  def _instanciate?
-    @enter.push Thread.current[:i]
-    while false.equal?(@__instance__)
-      Thread.critical = false
-      sleep 0.08 
-      Thread.critical = true
+    @valid = false
+    sleep(rand(0.1))
+    if self.class.attempts < 3
+      raise  "boom - initialize failed for thread ##{Thread.current[:i]}"
+    else
+      @valid = true
+      puts "yes! - initialize succceeded for thread ##{Thread.current[:i]}"
     end
-    @leave.push Thread.current[:i]
-    @__instance__
+  ensure
+    self.class.attempts+= 1
   end
   
-  def __sleep
-    sleep(rand(0.08))
-  end
+  def valid?
+    @valid
+  end  
+end
   
-  def new
-    begin
-      __sleep
-      raise  "boom - thread ##{Thread.current[:i]} failed to create instance"
-    ensure
-      # simple flip-flop
-      class << self
-        remove_method :new
-      end
-    end
-  end
+class << Foo
+  attr_accessor :attempts
   
   def instanciate_all
-    @enter = []
-    @leave = []
-    1.upto(9) {|i|  
-      Thread.new { 
+    Thread.current.priority = 101
+    thrs= Array.new(101) {|i|  
+      curr= Thread.new { 
         begin
+          sleep(rand(0.1))
           Thread.current[:i] = i
-          __sleep
           instance
-        rescue RuntimeError => mes
+        rescue => mes
           puts mes
         end
       }
+      curr.priority = rand(101)
+      curr
     }
-    puts "Before there were #{num_of_instances(self)}"
+    puts "Before there existed #{num} valid #{self} instance(s)"
     sleep 3
-    puts "Now there is #{num_of_instances(self)}"
-    puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
-    puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
+    thrs.each {|t| t.join }
+    puts "Now there exist(s) #{num} valid #{self} instance(s)"
   end
-end
+  
+  def num
+    cnt = 0
+    ObjectSpace.each_object(self) {|o| cnt+=1 if o.valid? }
+    cnt
+  end
+  
+  private
+  
+  def initialize_copy(orig)
+    super
+    @attempts = 0
+  end
+end  
 
 
-Ups.instanciate_all
-# results in message like
-# Before there were 0 Ups instance(s)
-# boom - thread #6 failed to create instance
-# initialize called by thread #3
-# Now there is 1 Ups instance(s)
-# 3; 2; 1; 8; 4; 7; 5 was the order of threads entering the waiting loop
-# 3; 2; 1; 7; 4; 8; 5 was the order of threads leaving the waiting loop
+Foo.instanciate_all
+# results in something like:
+# Before there were 0 valid Foo instance(s)
+# boom - initialize failed for thread #78
+# boom - initialize failed for thread #93
+# boom - initialize failed for thread #8
+# yes - initialize succceeded for thread #27
+# Now there are 1 valid Foo instance(s)
+# Now there are 1 valid Foo instance(s)
 
 
 puts "\nLets see if class level cloning really works"
-Yup = Ups.clone
-def Yup.new
-  begin
-    __sleep
-    raise  "boom - thread ##{Thread.current[:i]} failed to create instance"
-  ensure
-    # simple flip-flop
-    class << self
-      remove_method :new
-    end
-  end
-end
-Yup.instanciate_all
+Baz = Foo.clone
+Baz.instanciate_all
 
 


-- 
Nobu Nakada

In This Thread

Prev Next