valmikroy
10/7/2016 - 10:27 PM

ruby yield

ruby yield

#!/usr/bin/env ruby
#
#

proto =  %w(http https).freeze

@defaults = {
    :suit => 'wrk',
    :description => nil,
    :config => 'filepath',
    :commands => [],
    :options => {
      :wrk_command => '/bin/fake',
      :connections => { :start => 100 , :end => 500, :steps => 100 },
      :duration => 55,
      :threads => 36,
      :keepalive => nil,
      :http => {
        :port => 35325
      },
      :https => {
        :port => 3135135,
        :reuse => nil
      },
      :endpoints => nil
    }
}




def build_url(proto,host,port)
>-------return '  ' + proto + '://' + host + ':' + port.to_s
end


def build_args(proto)

    testbed_host = 'sadas.sda.sa'
    # yield just take code block and execute with argumetns.
    z =  yield  proto,testbed_host 
    puts z

end

build_args('http') do |p,h|  # <--- these are arguments which will provided to code block inside of function, check L40
    port = @defaults[:options][p.to_sym][:port]
    build_url(p,h,port)
end