为什么我的 Ruby / Sinatra 方法没有将所有字符串打印到屏幕上?
Why do my Ruby / Sinatra methods not print all the strings to the screen?
我在业余时间编写一个游戏来娱乐,并试图向我的老师炫耀我的知识。当我在 sinatra 中调用这个方法时,它知道你选择了多少次点击,如果它超过 2,它只会将 1 次点击添加到你的酸量中,然后告诉你它添加了多少。但是,如果您选择超过 2 个匹配项,它还应该告诉您只能从 1 或 2 个开始,并显示消息说明。但是它没有显示第一条消息或第二条消息。它只是告诉消息说明添加了 1 次或 2 次匹配。关于为什么或如何解决它的任何想法?
我的后端
class Trippy_methods
def start_with_amount(hits)
"You wanna start with 1 or 2 hits?"
@acid_amount = 0
if hits == 2
@acid_amount += 2
else @acid_amount += 1
"You may only start with 1 or 2 Hits...Ill give you the 1 hit for now. Don't Worry you will get more as time progresses "
end
"You Started off with taking #{@acid_amount} hits of acid....Enjoy"
end
end
在我的前端
post '/stick_out_tounge' do
tounge = params[:tounge]
name = session[:name]
session[:hits] = params[:hits]
hits = session[:hits].to_i
if tounge == "Yes"
erb :stick_out_your_tounge, :locals => {:yes => you_say.ok_to_acid, :places => trippy_messages.places_acid_on_tounge, :leave => trippy_messages.dosed_now_leave?,
:start_amount => trippy_methods.start_with_amount(hits) }
elsif tounge == "No"
erb :chillathome2, :locals => {:message1 => "You Say \" Gee I don't know man why what is it ?......\"",
:stick_out_tounge => "Dave says to you... \"Hey #{session[:name]} if you need to know than it wont be as fun..... \"",
:doyou => "Do you still wanna know what it is ?"}
end
end
ruby 中的方法总是 return 最后计算的表达式。因此,在您的示例中,您有
def start_with_amount(hits)
"You wanna start with 1 or 2 hits?"
@acid_amount = 0
if hits == 2
@acid_amount += 2
else @acid_amount += 1
"You may only start with 1 or 2 Hits...Ill give you the 1 hit for now. Don't Worry you will get more as time progresses "
end
"You Started off with taking #{@acid_amount} hits of acid....Enjoy" # <- This is the last evaluated expression.
end
所以无论方法做什么,它都会总是 return "You Started off with taking #{@acid_amount} hits of acid....Enjoy"
解决方法是将""You wanna start with 1 or 2 hits?"
移出方法,单独发送给视图,在用户选择自己想要的命中数之前显示出来。
要解决其他消息不显示的问题,您需要解决您的问题。
def start_with_amount(hits)
@acid_amount = 0
result = "You Started off with taking #{@acid_amount} hits of acid....Enjoy"
if hits == 2
@acid_amount += 2
elsif hits == 1
@acid_amount += 1
else
result = "You may only start with 1 or 2 Hits...Ill give you the 1 hit for now. Don't Worry you will get more as time progresses "
end
result
end
我在业余时间编写一个游戏来娱乐,并试图向我的老师炫耀我的知识。当我在 sinatra 中调用这个方法时,它知道你选择了多少次点击,如果它超过 2,它只会将 1 次点击添加到你的酸量中,然后告诉你它添加了多少。但是,如果您选择超过 2 个匹配项,它还应该告诉您只能从 1 或 2 个开始,并显示消息说明。但是它没有显示第一条消息或第二条消息。它只是告诉消息说明添加了 1 次或 2 次匹配。关于为什么或如何解决它的任何想法?
我的后端
class Trippy_methods
def start_with_amount(hits)
"You wanna start with 1 or 2 hits?"
@acid_amount = 0
if hits == 2
@acid_amount += 2
else @acid_amount += 1
"You may only start with 1 or 2 Hits...Ill give you the 1 hit for now. Don't Worry you will get more as time progresses "
end
"You Started off with taking #{@acid_amount} hits of acid....Enjoy"
end
end
在我的前端
post '/stick_out_tounge' do
tounge = params[:tounge]
name = session[:name]
session[:hits] = params[:hits]
hits = session[:hits].to_i
if tounge == "Yes"
erb :stick_out_your_tounge, :locals => {:yes => you_say.ok_to_acid, :places => trippy_messages.places_acid_on_tounge, :leave => trippy_messages.dosed_now_leave?,
:start_amount => trippy_methods.start_with_amount(hits) }
elsif tounge == "No"
erb :chillathome2, :locals => {:message1 => "You Say \" Gee I don't know man why what is it ?......\"",
:stick_out_tounge => "Dave says to you... \"Hey #{session[:name]} if you need to know than it wont be as fun..... \"",
:doyou => "Do you still wanna know what it is ?"}
end
end
ruby 中的方法总是 return 最后计算的表达式。因此,在您的示例中,您有
def start_with_amount(hits)
"You wanna start with 1 or 2 hits?"
@acid_amount = 0
if hits == 2
@acid_amount += 2
else @acid_amount += 1
"You may only start with 1 or 2 Hits...Ill give you the 1 hit for now. Don't Worry you will get more as time progresses "
end
"You Started off with taking #{@acid_amount} hits of acid....Enjoy" # <- This is the last evaluated expression.
end
所以无论方法做什么,它都会总是 return "You Started off with taking #{@acid_amount} hits of acid....Enjoy"
解决方法是将""You wanna start with 1 or 2 hits?"
移出方法,单独发送给视图,在用户选择自己想要的命中数之前显示出来。
要解决其他消息不显示的问题,您需要解决您的问题。
def start_with_amount(hits)
@acid_amount = 0
result = "You Started off with taking #{@acid_amount} hits of acid....Enjoy"
if hits == 2
@acid_amount += 2
elsif hits == 1
@acid_amount += 1
else
result = "You may only start with 1 or 2 Hits...Ill give you the 1 hit for now. Don't Worry you will get more as time progresses "
end
result
end