ruby - 练习 50::http://ruby.learncodethehardway.org/book/ex50.html(使用 sinatra 运行 hello world 时出错”

标签 ruby sinatra

我正在尝试 ex:50 of learning Ruby the hard way ..其中涉及使用“sinatra”创建 hello_world 应用程序 我收到如下错误:

 ruby lib/gothonweb.rb
    lib/gothonweb.rb:5:in `<module:Gothonweb>': undefined method `get' for Gothonweb:Module             (NoMethodError)
    from lib/gothonweb.rb:4:in `<main>'

最佳答案

这不是您所做的事情,给出的代码不起作用:

require_relative "gothonweb/version"
require "sinatra"

module Gothonweb
  get '/' do
    greeting = "Hello, World!"
    return greeting
  end
end

这不起作用,因为它被包装在模块中。试试这个:

require_relative "gothonweb/version"
require "sinatra"

get '/' do
  greeting = "Hello, World!"
  greeting # there's no need for the return here,
           # the last expression is the return value
end

关于ruby - 练习 50::http://ruby.learncodethehardway.org/book/ex50.html(使用 sinatra 运行 hello world 时出错”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16645762/

相关文章:

ruby - 如何重构 API 的 RSpec 测试

ruby - `binding.pry` 与 `pry`

ruby - 默认 : "now()" in Sequel generates literal embedded into the defenitiion of a table

ruby - 使用 Sinatra 和 MongoDB - "keep alive"http 请求之间的 mongodb 连接的推荐方法是什么?

testing - 转换为模块化 sinatra 应用程序中断测试

ruby-on-rails - 带有 html 标签的 erb 中的 Ruby 三元运算符

ruby - 为了开始学习 Sinatra,我需要学习多少 Ruby

ruby - Notepad++ 只是调用 "ANSI"的编码,有谁知道在 Ruby 中如何调用它?

ruby - 初始化哈希

ruby - 是否可以在 Ruby 中创建(或模拟)关键字?