I've got a database with posts named line1, line2, line3, line4, line5, line6.

I've got a

x = rand(6)+1 

to obtain the random value. Now I would like the "line"+x in some way such as this:

y = @variable."line"+x.to_s

And obtain back the information in lineX. What is the right formula? Texas!

Presuming you possess an ActiveRecord model for the database table. The next should work:

x = rand(6)+1 
@record = MyModel.find(1)
y = @record["line#{x}"]

Only use send approach to Object:

y = @variable.send("line#{x}")