如何创建一个 ruby​​ Hello world?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/705729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:05:31  来源:igfitidea点击:

How do I create a ruby Hello world?

ruby-on-railsruby

提问by toddoon

I know in PHP you have to intrepret a page like index.php, but in Ruby how does it work? I don't know what is the Ruby extension like index.php for PHP. Could you help me?

我知道在 PHP 中你必须解释一个像 index.php 这样的页面,但在 Ruby 中它是如何工作的?我不知道 PHP 的 index.php 之类的 Ruby 扩展是什么。你可以帮帮我吗?

回答by ScArcher2

If you are talking about a command line program this will work.

如果您在谈论命令行程序,这将起作用。

puts "Hello World"

or if you want an object oriented version

或者如果你想要一个面向对象的版本

class HelloWorld
   def initialize(name)
      @name = name.capitalize
   end
   def sayHi
      puts "Hello #{@name}!"
   end
end

hello = HelloWorld.new("World")
hello.sayHi

If you are looking for a ruby on rails version of Hello World. Check the Getting Started Guidefor Rails.

如果您正在寻找 ruby​​ on rails 版本的 Hello World。查看Rails入门指南

回答by thijs

You can take a look at this Ruby Programming Wikion Wikibooks

您可以在 Wikibooks 上查看这个Ruby Programming Wiki

Code:

代码:

puts 'Hello world'

Run:

跑:

$ ruby hello-world.rb
Hello world

回答by the Tin Man

This is how to write a very simple "hello world" using Sinatra, which is a great way to bring up a Ruby-based website without using Rails. The sample is basically the same as the Sinatra folks have on the front page of their site. It's really this simple.

这是使用Sinatra编写一个非常简单的“hello world”的方法,这是在不使用 Rails 的情况下创建基于 Ruby 的网站的好方法。该示例与 Sinatra 人员在其网站首页上的示例基本相同。真的就是这么简单。

Install the Sinatra gem along with its dependencies:

安装 Sinatra gem 及其依赖项:

`gem install sinatra`

Save this to a file called hi.rb:

将其保存到名为 的文件中hi.rb

require 'sinatra'

get '/hi' do
  "Hello World!"
end

Drop to the command-line, and enter ruby hi.rb. After a few seconds you should see something like:

放到命令行,然后输入ruby hi.rb. 几秒钟后,您应该会看到如下内容:

== Sinatra/1.1.0 has taken the stage on 4567 for development with backup from WEBrick
[2010-12-04 11:43:43] INFO  WEBrick 1.3.1
[2010-12-04 11:43:43] INFO  ruby 1.9.2 (2010-08-18) [x86_64-darwin10.5.0]
[2010-12-04 11:43:43] INFO  WEBrick::HTTPServer#start: pid=37898 port=4567:

By default Sinatra serves its pages at port=4567, but you can change it. Read the docsto learn how.

默认情况下,Sinatra 在 上提供其页面port=4567,但您可以更改它。阅读文档以了解如何操作。

Open a new window in your browser, and go to:

在浏览器中打开一个新窗口,然后转到:

http://localhost:4567/hi

and you should see Hello World!in your browser window.

您应该会Hello World!在浏览器窗口中看到。

Sinatra is really easy to work with, and makes a great prototyping and light-to-medium weight MVC-like server. I love it because of its easy integration with Sequel, my favorite ORM, and HAML, which replaces ERB as the templating engine.

Sinatra 真的很容易使用,并且是一个很棒的原型设计和轻到中等重量的类似 MVC 的服务器。我喜欢它,因为它可以轻松与Sequel、我最喜欢的 ORM 和HAML集成,后者取代了 ERB 作为模板引擎。

Sinatra's Intro docis a great starting point. The Sinatra Bookis a good resource too.

Sinatra 的Intro doc是一个很好的起点。该西纳特拉本书是一个很好的资源太多。

回答by d1jhoni1b

How does it work in Ruby?

它在 Ruby 中是如何工作的?

Ruby is a scripting language (not compiled) just like php (as you said "you have to intrepet a page") and python, bin/bash, etc...in Ruby you have libraries with helpers and very very cool stuff they are called "gems" (Ruby and Gems :D nice name convention right? BTW this is because Ruby's parent is Perl).

Ruby 是一种脚本语言(未编译),就像 php(正如你所说的“你必须插入一个页面”)和 python、bin/bash 等......在 Ruby 中,你有带有帮助程序的库和非常酷的东西称为“ gems”(Ruby 和 Gems :D 不错的命名约定对吗?顺便说一句,这是因为 Ruby 的父级是 Perl)。

You can organized different files inside one Ruby's project folder, it could be in this case one *.rb file and one "Gemfile" (that's the name without extension) in which you define which "gems" you want to install in your Ruby app (read about bundler), only with this two files you will be able to successfully do anything you want butas a desktop app(by this i mean that the Ruby app you write will only be executable on a computer with Ruby installed, and you have to install it manually (with bundlerso all required "gems" are in there) and then manually run Ruby's command targeting your code's main class (unless of course you create a cron-jobthat do this automatically for you, pretty common practice to run processes on web servers).

您可以在一个 Ruby 的项目文件夹中组织不同的文件,在这种情况下可以是一个 *.rb 文件和一个“ Gemfile”(这是没有扩展名的名称),您可以在其中定义要在 Ruby 应用程序中安装的“gems” (阅读bundler),只有使用这两个文件,您才能成功地做任何您想做的事情,作为桌面应用程序(我的意思是,您编写的 Ruby 应用程序只能在安装了 Ruby 的计算机上执行,并且您必须手动安装它(使用捆绑器,所以所有必需的“ gems”都在那里)然后手动运行针对代码主类的 Ruby 命令(当然除非你创建了一个cron-job自动为您执行此操作,这是在 Web 服务器上运行进程的常见做法)。

If you want to use Ruby to create a "webapp" , website , etc right now two pretty popular choices are using the "rails" framework and "sinatra" gem.

如果您现在想使用 Ruby 来创建“ webapp”、网站等,两个非常流行的选择是使用“ rails”框架和“ sinatra”gem。

With rails (that's why you hear much about ruby on rails) framework you are able to execute commands to create new website project, remember that rails uses the coding pattern called MVC(model view controller) so you will have plenty options for creating your models, views and controllers individually or using "scaffold" that will create all of them for you, rails will create a bunch of files and some of them will not be *.rb of Gemfile, all of them will have a specific task: configuration files for database, labels, of config or other "gems" you install besides rails.Take in mind that rails offer stuff for TDD(test driven development) so in a matter of hours you can have a fully functional website 100% tested and operational (big infrastructure).

使用 rails(这就是为什么您经常听到有关 ruby​​ on rails 的原因)框架,您可以执行命令来创建新的网站项目,请记住,rails 使用称为MVC(模型视图控制器)的编码模式,因此您将有很多选项来创建模型,视图和控制器单独或使用“脚手架”为您创建所有这些,rails 将创建一堆文件,其中一些文件不是 Gemfile 的 *.rb,它们都有一个特定的任务:配置文件用于数据库、标签、配置或除了 rails 之外安装的其他“gems”。请记住,rails 提供用于TDD(测试驱动开发)的东西,因此在几个小时内,您可以拥有一个功能齐全的网站,100% 测试和运行(大型基础设施)。

This is why i also brought "sinatra" gem to this conversation...sinatra will give you same functionality than rails does but instead sinatra will not install anything for you (leaving space for errorif you have not expertise on setting on web servers, web apps , etc) only the sinatra framework which will run a server for you on a specific port number so that way you can then add code to your main class in order to display HTML(small infrastructure)

这就是为什么我还把“sinatra” gem 带到了这个对话中……sinatra 会给你与 rails 相同的功能,但 sinatra 不会为你安装任何东西(如果你没有设置网络服务器的专业知识,就会留下错误的空间, Web 应用程序等)只有 sinatra 框架,它会在特定端口号上为您运行服务器,这样您就可以将代码添加到主类中以显示 HTML(小型基础设施

What is the Ruby extension like index.php for PHP?

PHP 的 index.php 之类的 Ruby 扩展是什么?

All ruby files are using *.rb

所有 ruby​​ 文件都使用 *.rb

Hope this helps!

希望这可以帮助!

PS: Hello world sample

PS:你好世界样本

  1. install ruby
  2. create a new folder an inside create a file "hello.rb"
  3. open the file and add the following code:

    puts 'Hello world'

  4. close and save the file

  5. now open a terminal, console, etc go to your ruby file folder path and run the following command:

    ruby hello.rb

  6. that will print on your console:

    Hello world

  1. 安装红宝石
  2. 创建一个新文件夹并在里面创建一个文件“hello.rb”
  3. 打开文件并添加以下代码:

    输入“你好世界”

  4. 关闭并保存文件

  5. 现在打开终端、控制台等,转到您的 ruby​​ 文件夹路径并运行以下命令:

    红宝石你好.rb

  6. 这将打印在您的控制台上:

    你好,世界

回答by jeffcook2150

puts "Hello, World!"

To run Ruby scripts on the web, you need to use a special server, run through (F)CGI, or do some other stuff; there are several ways to get different languages HTTP-accessible. However, the simplest way is probably to use a Ruby web framework, such as Ruby on Rails or Merb -- these projects include servers and all of the things you need to get going.

要在 web 上运行 Ruby 脚本,你需要使用一个特殊的服务器,通过 (F)CGI 运行,或者做一些其他的事情;有几种方法可以使不同语言的 HTTP 可访问。然而,最简单的方法可能是使用 Ruby Web 框架,例如 Ruby on Rails 或 Merb —— 这些项目包括服务器和所有您需要的东西。

回答by Zeck

Just copy and past this code on your terminal. Then hit enter.

只需在您的终端上复制并粘贴此代码即可。然后按回车。

ruby -e "puts 'Hello world'"

回答by Brian R. Bondy

I know the question was talking about Ruby, but I think you meant rails (which is what it was tagged as). Rails is a web framework that uses the ruby programming language.

我知道这个问题是在谈论 Ruby,但我认为你的意思是 Rails(这就是它的标签)。Rails 是一个使用 ruby​​ 编程语言的 Web 框架。

install rubyonrails.

安装 ruby​​onrails。

Type:

类型:

rails projectname
cd projectname
ruby script/server

Navigate to http://localhost:3000

导航到http://localhost:3000