Don't know what the term is called (substitution?), but in python if you type
num1 = 4
num2 = 2
print("Lucky numbers: %d %d" %(num1, num2))
You get "Lucky numbers: 4 2"
How do I do this in ruby?
Trying to do the above scenario, it works if I have one variable, but if I need to sub in multiple variables the parentheses aren't valid syntax.
Ruby allows part of a string to be modified through the use of the []= method. To use this method, simply pass through the string of characters to be replaced to the method and assign the new string.
gsub! is a String class method in Ruby which is used to return a copy of the given string with all occurrences of pattern substituted for the second argument. If no substitutions were performed, then it will return nil. If no block and no replacement is given, an enumerator is returned instead.
In Ruby, we can permanently delete characters from a string by using the string. delete method. It returns a new string with the specified characters removed.
Ruby | Matrix tr() function Return Value: It returns the trace.
You can use something called string interpolation in Ruby to accomplish this.
ex:
num1 = 4
num2 = 2
puts "Lucky numbers: #{num1} #{num2}";
Here each variable that is inside the #{} is interpreted not as a String but as a variable name and the value is substituted.
num1 = 4
num2 = 2
print "Lucky numbers: %d %d" % [num1, num2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With