I have this code:
on :message, "something" do |m|
m.reply file.read.lines[2]
end
...which works, but only once. When I try it again or use the same code but with a different file, it doesn't work. Can someone help me do this?
I have this code:
on :message, "something" do |m|
m.reply file.read.lines[2]
end
...which works, but only once. When I try it again or use the same code but with a different file, it doesn't work. Can someone help me do this?
The reason you're getting this behavior depends on how you're defining file
.
But with no other information it's still possible to give a (hopefully) working example:
on :message, "something" do |m|
first_line_to_read = 0
last_line_to_read = 2
lines_of_text = file.read.split("\n")
first_line_to_read.upto(last_line_to_read).each do |idx|
m.reply lines_of_text[idx]
end
end
Hopefully this example is clear. It sends separate replies for each line of the text that is within the bounds of the first_line_to_read
and last_line_to_read
indexes.
Some important concepts are:
file.read
multiple times, this will store the result of calling it the first time. m
variable which is defined in parent scope to send the message.