Метод Ruby может уступить как итератор или возвратить массив в зависимости от контекста?

с использованием jQuery:

$(function() {
    function unifyHeights() {
        var maxHeight = 0;
        $('#container').children('#navigation, #content').each(function() {
            var height = $(this).outerHeight();
            // alert(height);
            if ( height > maxHeight ) {
                maxHeight = height;
            }
        });
        $('#navigation, #content').css('height', maxHeight);
    }
    unifyHeights();
});
10
задан sh-beta 26 June 2009 в 18:03
поделиться

2 ответа

There is a syntax for that:

def arbitrary(&block)
  values = [1, 2, 3, 4]
  if block
    values.each do |v|
      yield v
    end
  else
    values
  end
end

Note:

yield v

Can be replaced with:

block.call v
14
ответ дан 3 December 2019 в 13:35
поделиться
def arbitrary
  values = [1,2,3,4]
  return values unless block_given? 
  values.each { |val| yield(val) }
end
arbitrary { |x| puts x }
arbitrary
19
ответ дан 3 December 2019 в 13:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: