Możesz użyć formatowania podobnego do sprintf, aby wstrzyknąć wartości do ciągu. W tym celu ciąg musi zawierać symbole zastępcze. Umieść swoje argumenty w tablicy i użyj jednego z następujących sposobów: (Aby uzyskać więcej informacji, zajrzyj do dokumentacji Kernel :: sprintf .)
fmt = 'The %s %s the %s'
res = fmt % [animal, action, other_animal] # using %-operator
res = sprintf(fmt, animal, action, other_animal) # call Kernel.sprintf
Możesz nawet jawnie określić numer argumentu i przetasować go wokół:
'The %3$s %2$s the %1$s' % ['cat', 'eats', 'mouse']
Lub określ argument za pomocą kluczy z krzyżykiem:
'The %{animal} %{action} the %{second_animal}' %
{ :animal => 'cat', :action=> 'eats', :second_animal => 'mouse'}
Zauważ, że musisz podać wartość dla wszystkich argumentów dla %operatora. Na przykład nie możesz uniknąć definiowania animal.