Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Talking this is what I wanted the code to do


    this is what I wanted the ruby programming code to do, while quoting yoda in the programming code

    #!/usr/bin/ruby
    options = {}
    options[:do] = true
    options[:do_not] = true
    options[:try] = false
    (:try)
    require 'set'
    s = Set.new ['1.1.1.1', '1.2.3.4']
    # => #<Set: {"1.1.1.1", "1.2.3.4"}>
    s.include? '1.1.1.1'
    # => true
    x=1
    if x > 2
    puts "do"
    elsif x <= 2 and x!=0
    puts "there is no try"
    else
    puts "or do not"
    end
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0
    !(myarray & ["val1", "val2", "val3", "val4"]).empty?

    or
    Looping (note that you could create a dict/set of the check vals to make the inclusion test in the block O(1)):

    if myarray.include? 'val1' ||
    myarray.include? 'val2' ||
    myarray.include? 'val3' ||
    myarray.include? 'val4'
    ....

    by:
    bdeveloper01
  4. #18
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    What does this have to do with the thread?

    Apart from that, the second variant can't be parsed. I guess you meant the low-precedence "or" instead of "||" (this isn't Java).

    And the first one looks pretty obscure and inefficient to me (you're calculating the complete intersection just for checking if there's a common value). I think a more "Rubyish" variant would be
    Code:
    ['val1', 'val2', 'val3', 'val4'].any? {|element| myarray.include? element}
    This is also a lot faster, because it stops at the first match.
  6. #19
  7. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0
    class RaitoSolution
    def initialize(origin, &block)
    @origin = origin
    self.class.send(:define_method, :dispatch, block)
    end
    end

    a = RaitoSolution.new(2){ @origin + 40}
    p a.dispatch # => 42
    b = RaitoSolution.new(40){ @origin + 2}
    p b.dispatch # => 42
    p a.dispatch # => 4
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo