Redefine while

Discussion in 'Ruby' started by Emil Sandin, Nov 6, 2008.

  1. Emil Sandin

    Emil Sandin Guest

    Hi, is there an easy way to redefine 'while'. I have a class that uses a
    while loop. In my unit test it always evaluates to false (which is
    correct). I only want the loop to run once, and the enter. Just like an
    if.

    This is the code I've tried:


    def while(condition)
    p "redefined while!"
    yield if condition
    end

    while true do
    p "hello"
    sleep 1
    end


    But it loops eternally.
    Any help?
    --
    Posted via http://www.ruby-forum.com/.
     
    Emil Sandin, Nov 6, 2008
    #1
    1. Advertising

  2. On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <> wrote:
    > Hi, is there an easy way to redefine 'while'. I have a class that uses a
    > while loop. In my unit test it always evaluates to false (which is
    > correct). I only want the loop to run once, and the enter. Just like an
    > if.
    >
    > This is the code I've tried:
    >
    >
    > def while(condition)
    > p "redefined while!"
    > yield if condition
    > end
    >
    > while true do
    > p "hello"
    > sleep 1
    > end
    >
    >
    > But it loops eternally.
    > Any help?

    The problem is that while is not a method in ruby but a keyword [reserved word].

    http://www.zenspider.com/Languages/Ruby/QuickRef.html

    Michael Guterl
     
    Michael Guterl, Nov 6, 2008
    #2
    1. Advertising

  3. On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <> wrote:
    > Hi, is there an easy way to redefine 'while'. I have a class that uses a
    > while loop. In my unit test it always evaluates to false (which is
    > correct). I only want the loop to run once, and the enter. Just like an
    > if.
    >
    > This is the code I've tried:
    >
    >
    > def while(condition)
    > p "redefined while!"
    > yield if condition
    > end
    >
    > while true do
    > p "hello"
    > sleep 1
    > end
    >
    >
    > But it loops eternally.
    > Any help?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Not possible. 'while' is a keyword, not a method.

    Jason
     
    Jason Roelofs, Nov 6, 2008
    #3
  4. Emil Sandin

    Emil Sandin Guest

    Jason Roelofs wrote:
    > On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <> wrote:
    >> yield if condition
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>
    >>

    >
    > Not possible. 'while' is a keyword, not a method.
    >
    > Jason


    Too bad. I thought you could do anything with ruby ;)

    Emil
    --
    Posted via http://www.ruby-forum.com/.
     
    Emil Sandin, Nov 6, 2008
    #4
  5. Emil Sandin

    Guest

    On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <> wrote:
    > Hi, is there an easy way to redefine 'while'. I have a class that uses a
    > while loop. In my unit test it always evaluates to false (which is
    > correct). I only want the loop to run once, and the enter. Just like an
    > if.


    As others already noted, while is a keyword not a method. But
    depending on how your code is structured you might be able to mock the
    condition in your test:

    $ cat foo.rb
    class Foo
    attr_reader :i

    def initialize
    @i = 0
    end

    def condition
    @i < 10
    end

    def method
    while condition
    @i += 1
    end
    end
    end

    $ cat foo_spec.rb
    require 'foo'

    describe Foo do
    it "should loop once" do
    f = Foo.new
    f.i.should == 0
    f.method
    f.i.should == 10
    end

    it "should loop once" do
    f = Foo.new
    f.should_receive:)condition).and_return(true, false)
    f.i.should == 0
    f.method
    f.i.should == 1
    end
    end

    $ spec foo_spec.rb
    ..
    Finished in 0.008461 seconds
    2 examples, 0 failures
     
    , Nov 6, 2008
    #5
  6. Emil Sandin

    Emil Sandin Guest

    A good idea, but isn't working in my case. If I mock the condition I
    change the behaviour of the method under test too much. But thanks
    anyway!
    --
    Posted via http://www.ruby-forum.com/.
     
    Emil Sandin, Nov 6, 2008
    #6
  7. Emil Sandin <> writes:

    > Jason Roelofs wrote:
    >> On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <> wrote:
    >>> yield if condition
    >>> --
    >>> Posted via http://www.ruby-forum.com/.
    >>>
    >>>

    >>
    >> Not possible. 'while' is a keyword, not a method.
    >>
    >> Jason

    >
    > Too bad. I thought you could do anything with ruby ;)


    No. You'd need a non-broken lisp to do that.

    In Common Lisp:

    (shadow 'while)
    (defmacro while (condition &body body)
    `(progn
    (format t "In redefined while~%")
    (when ,condition
    ,@body)))

    (while t
    'hi)
    In redefined while
    HI

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Nov 6, 2008
    #7
  8. On 06.11.2008 16:43, Emil Sandin wrote:
    > A good idea, but isn't working in my case. If I mock the condition I
    > change the behaviour of the method under test too much. But thanks
    > anyway!


    I find your approach suspicious: you write a class with a method whose
    regular behavior involves executing a loop. Then, during tests you want
    the loop to not loop, i.e. you change the behavior of your method.
    Strictly speaking the test result is then meaningless, because you do
    not test the behavior of the method that you want to use regularly.

    If there is a complex operation in the loop that you want to test
    separately you should probably refactor the code. Then you can have
    your one off test.

    Cheers

    robert
     
    Robert Klemme, Nov 6, 2008
    #8
  9. Pascal J. Bourguignon wrote:

    > Emil Sandin <> writes:
    >
    > > Jason Roelofs wrote:
    > >> On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <>

    > wrote: >>> yield if condition
    > >>> --
    > >>> Posted via http://www.ruby-forum.com/.
    > > > >
    > > > >
    > >>
    > >> Not possible. 'while' is a keyword, not a method.
    > >>
    > >> Jason

    > >
    > > Too bad. I thought you could do anything with ruby ;)

    >
    > No. You'd need a non-broken lisp to do that.


    No, you wouldn't. For real extensibility, you need Forth.

    >
    > In Common Lisp:


    A.k.a. Commune Lisp, Committee Lisp, and COBOL Lisp.

    >
    > (shadow 'while)
    > (defmacro while (condition &body body)
    > `(progn
    > (format t "In redefined while~%")
    > (when ,condition
    > ,@body)))
    >
    > (while t
    > 'hi)
    > In redefined while
    > HI


    GForth:

    variable x

    : try1 2 x !
    begin
    x @ 9 <
    while
    x @ .
    1 x +!
    repeat ;

    : repeat 2drop drop postpone then ; immediate

    : try2 2 x !
    begin
    x @ 9 <
    while
    x @ .
    1 x +!
    repeat ;

    try1 cr try2

    2 3 4 5 6 7 8 redefined REPEAT with repeat
    2
     
    William James, Nov 7, 2008
    #9
  10. William James wrote:

    > 2 3 4 5 6 7 8 redefined REPEAT with repeat
    > 2


    redefined REPEAT with repeat 2 3 4 5 6 7 8
    2
     
    William James, Nov 7, 2008
    #10
  11. Emil Sandin

    Emil Sandin Guest

    Robert Klemme wrote:
    > On 06.11.2008 16:43, Emil Sandin wrote:
    >
    > I find your approach suspicious: you write a class with a method whose
    > regular behavior involves executing a loop. Then, during tests you want
    > the loop to not loop, i.e. you change the behavior of your method.
    > Strictly speaking the test result is then meaningless, because you do
    > not test the behavior of the method that you want to use regularly.
    >
    > If there is a complex operation in the loop that you want to test
    > separately you should probably refactor the code. Then you can have
    > your one off test.
    >
    > Cheers
    >
    > robert


    That is probably a good idea.
    --
    Posted via http://www.ruby-forum.com/.
     
    Emil Sandin, Nov 7, 2008
    #11
  12. Emil Sandin

    Kaz Kylheku Guest

    On 2008-11-06, Pascal J. Bourguignon <> wrote:
    > Emil Sandin <> writes:
    >
    >> Jason Roelofs wrote:
    >>> On Thu, Nov 6, 2008 at 10:00 AM, Emil Sandin <> wrote:
    >>>> yield if condition
    >>>> --
    >>>> Posted via http://www.ruby-forum.com/.
    >>>>
    >>>>
    >>>
    >>> Not possible. 'while' is a keyword, not a method.
    >>>
    >>> Jason

    >>
    >> Too bad. I thought you could do anything with ruby ;)

    >
    > No. You'd need a non-broken lisp to do that.
    >
    > In Common Lisp:


    Now now, Pascal; William James is a Usenet problem, not a comp.lang.ruby
    problem. It doesn't make sense to retaliate in this way. :)
     
    Kaz Kylheku, Nov 7, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Bane
    Replies:
    0
    Views:
    595
    Michael Bane
    Sep 11, 2003
  2. Fernando Martins

    Redefine entities in XSL.

    Fernando Martins, Oct 1, 2003, in forum: XML
    Replies:
    0
    Views:
    465
    Fernando Martins
    Oct 1, 2003
  3. BillD
    Replies:
    2
    Views:
    834
    BillD
    Oct 4, 2004
  4. Cat
    Replies:
    1
    Views:
    552
  5. Nafai
    Replies:
    4
    Views:
    345
    James Rafter
    Dec 8, 2004
Loading...

Share This Page