Is there any way to embed a block inside a string in a way that is executable like this?
for i in 1..1000
test "There once was #{if i=1{'a man'} else {'many men'} end} from kilkenny"
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, everything inside of
#{}will be evaluated before the string is used, and therefore has to be valid Ruby code. Try:Here, I’ve used the ternary operator, used in Ruby and other languages, which is basically:
Also note the double equal
==, which is the equality operator in Ruby. Single equal,=is the assignment operator, which in your code resets the value ofito1in every loop.Because all values except nil and false are truthy in Ruby, your proposed if .. then will always be true, because
i=1returns1, which is true.