我正在业余时间编写游戏以获得乐趣,并试图向老师展示我的知识。当我在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
答案 0 :(得分:0)
ruby中的方法总是返回最后一次计算的表达式。所以,在你的例子中你有
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
所以无论方法做什么,总是返回"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