method that generate block?

Discussion in 'Ruby' started by Nanyang Zhan, Jan 9, 2008.

  1. Nanyang Zhan

    Nanyang Zhan Guest

    Hi, could anyone tell me how to code a method/methods that generates
    these blocks? thanks.
    vds[0].each do |d0|
    vds[1].each do |d1|
    ...
    vds[n].each do |dn|
    ds = [d0, d1..., dn]
    create_new_filter(ds)
    end
    end
    ...
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Nanyang Zhan, Jan 9, 2008
    #1
    1. Advertising

  2. Nanyang Zhan

    Ckvok Kovsky Guest

    0..10.times do |time|
    vds[time].each do |my_var|
    ds = << my_var
    end
    end

    create_new_filter(ds)

    -----------------
    I didn't test.
    --
    Posted via http://www.ruby-forum.com/.
     
    Ckvok Kovsky, Jan 9, 2008
    #2
    1. Advertising

  3. Nanyang Zhan

    Nanyang Zhan Guest

    Ckvok Kovsky wrote:
    > 0..10.times do |time|
    > vds[time].each do |my_var|
    > ds = << my_var
    > end
    > end
    >
    > create_new_filter(ds)
    >
    > -----------------
    > I didn't test.


    thanks,but yours will only create one filter, and I want to create
    vds[0].size*vds[1].size*...*vds[n].size filters, basing on every ds
    value. Each ds value is a arrary that contains one value from the vds
    array.

    I mean this:
    n = vds.size
    dss = []
    vds[0].each do |d0|
    vds[1].each do |d1|
    ...
    vds[n].each do |dn|
    dss << [d0, d1, ... , dn]
    end
    ...
    end
    end

    dss.each {|ds| create_filter(ds) }

    --
    Posted via http://www.ruby-forum.com/.
     
    Nanyang Zhan, Jan 10, 2008
    #3
  4. Nanyang Zhan

    Nanyang Zhan Guest

    > I mean this:
    > n = vds.size
    > dss = []
    > vds[0].each do |d0|
    > vds[1].each do |d1|
    > ...
    > vds[n].each do |dn|
    > dss << [d0, d1, ... , dn]
    > end
    > ...
    > end
    > end



    OK, now I know I just need a cartesian product method and I've got one
    from http://www.ruby-forum.com/topic/95519#615075 :

    def cart_prod( *args )
    args.inject([[]]){|old,lst|
    new = []
    lst.each{|e| new += old.map{|c| c.dup << e }}
    new
    }
    end

    The problem is this method take arrays as argument, how can I pass into
    only one array as argument? (this one array will contains sub arrays
    that I want to make a cartesian product from, for example: vds =
    [[1,2],["a","b"]], how to pass the vds array into the method to get
    cartesian product of [1,2] and ["a","b"]?)

    --
    Posted via http://www.ruby-forum.com/.
     
    Nanyang Zhan, Jan 10, 2008
    #4
  5. > def cart_prod( *args )
    > args.inject([[]]){|old,lst|
    > new = []
    > lst.each{|e| new += old.map{|c| c.dup << e }}
    > new
    > }
    > end
    >
    > The problem is this method take arrays as argument, how can I pass into
    > only one array as argument? (this one array will contains sub arrays
    > that I want to make a cartesian product from, for example: vds =
    > [[1,2],["a","b"]], how to pass the vds array into the method to get
    > cartesian product of [1,2] and ["a","b"]?)


    Remove the * from *args.

    Paolo
     
    Paolo Bonzini, Jan 10, 2008
    #5
  6. Nanyang Zhan

    Nanyang Zhan Guest

    Nanyang Zhan, Jan 10, 2008
    #6
    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. Weng Tianxiang
    Replies:
    5
    Views:
    1,317
    Christophe
    Feb 16, 2006
  2. morrell
    Replies:
    1
    Views:
    962
    roy axenov
    Oct 10, 2006
  3. Replies:
    3
    Views:
    415
    red floyd
    Apr 7, 2006
  4. John J. Franey
    Replies:
    3
    Views:
    133
    George
    Jun 15, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    208
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page