ruby on rails - Only give method block and yield if block_given? -


let's have basic td_tag helper wraps content_tag method in application_helper.rb

def td_tag(*args)   if block_given?     content_tag(:td, *args)       yield     end   else     content_tag(:td ,*args)   end end 

what ruby-way of making code shorter?

this example works, in way, doesn't seem nessesary call same function same parameters, , without block?

by appending block_given? yield row inside block doesn't work, since have given next function block work with.

accept block argument , pass through. no need use block_given?.

this functionally identical code:

def td_tag(*args, &block)   content_tag(:td, *args, &block) end 

it's safe; if not give method block, block nil , block_given? inside content_tag false.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -