RxRuby icon indicating copy to clipboard operation
RxRuby copied to clipboard

Change order of parameters on Observable.while

Open mattpodwysocki opened this issue 11 years ago • 4 comments

@jca02266 should we change the order of the parameter on #while so that we could use a block instead?

require 'rx'

i = 0

# Repeat until condition no longer holds
source = RX::Observable.while(RX::Observable.just 42) { i += 1; i <= 3 }

subscription = source.subscribe(
    lambda {|x|
        puts 'Next: ' + x.to_s
    },
    lambda {|err|
        puts 'Error: ' + err.to_s
    },
    lambda {
        puts 'Completed'
    })

# => Next: 42
# => Next: 42
# => Next: 42
# => Completed

mattpodwysocki avatar Dec 31 '14 20:12 mattpodwysocki

It seems like: while(condition) { action } but not,

IMHO, change the method name to something for ruby way. We must careful to use block.

jca02266 avatar Dec 31 '14 23:12 jca02266

How about yield_while instead?

mattpodwysocki avatar Dec 31 '14 23:12 mattpodwysocki

We can accept block without changing parameter order

--- a/lib/rx/linq/observable/while.rb
+++ b/lib/rx/linq/observable/while.rb
@@ -1,6 +1,13 @@
 module RX
   class <<Observable
-    def while(condition, source)
+    def while(condition_or_source, source = nil)
+      if block_given?
+        condition = Proc.new
+        source = condition_or_source
+      else
+        condition = condition_or_source
+      end
+
       enum = Enumerator.new {|y|
         while condition.call
           y << source

We have many time considering it

jca02266 avatar Dec 31 '14 23:12 jca02266

How about yield_while instead?

Hmm.

jca02266 avatar Dec 31 '14 23:12 jca02266