Why does this only output the x value?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this only output the x value??

x = gets for y in (1..12) z = x * y puts z end this is a code which i created for multiplication table. but this only outputs the input x. help me to make this work

7th Nov 2016, 4:52 AM
Nayomal
Nayomal - avatar
3 Answers
+ 1
x = gets x = x.to_i for y in (1..12) z = x * y puts "#{x} * #{y} = #{z}" end you need to convert x to number because the interpreter thinks it's a string so "5"' * 1 = "5" and "5" * 2 = "55" (each digit printed on a new line)
7th Nov 2016, 5:47 AM
Blazing Code
Blazing Code - avatar
+ 1
no problem, its called string interpolation, what it does is it says get the variable between these curly braces and put its value here in the string, eg message = "hello" puts "#{message} world" # hello world message = "good afternoon" puts "#{message} world" # good afternoon world alternatively you can just concatenate them like this: message + " world" but when you introduce more variables it gets more complicated to do so which is why people like to use interpolation to make things easier
8th Nov 2016, 6:38 AM
Blazing Code
Blazing Code - avatar
0
y are those in between curly brakets? i am just new to coding. so please :) ur helps are valuable
8th Nov 2016, 12:51 AM
Nayomal
Nayomal - avatar