Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

rubyの出力(hello world)

Last updated at Posted at 2020-12-02

!Mac OS X-10.13.3 !ruby-2.7.0p0

rubyの入出力について,とりあえず何も考えずに世の中に挨拶する文言をターミナルに出力します.最終目標はターミナルで名前を入れてその人に挨拶.

出力について

rubyの出力には`p`,`puts`,`print`があり,requireすることでC言語的な`printf`も使える.とりあえずは世の中に挨拶するので,

hello.rb
p "Hello World."

とすると,

> ruby hello.rb
Hello World.

これで,ただただベタ打ちの出力ができました.

入力について

入力は`gets`やrequireで`scanf`なども使えるが,今回はARGVを使います.ARGVはコマンドラインから呼び出すときの入力を読み取るものです.

get_string.rb
string = ARGV[0]
p string

とすると,

> ruby get_string.rb hogeo
hogeo

と表示されます.これで入力はできるので次は最終目標.

入力した名前に挨拶

目標は

ruby hello_name.rb Rudy

とした時に,

Hello Rudy.

と表示させたい.

codeは

if ARGV[0]
  puts "Hello #{ARGV[0]}."
else
  puts "Hello world."
end

これでもし名前を打たなくてもエラーとかなく表示できるでしょう.


  • source ~/Github/grad_members_20f/members/yamatoken/items/c1_hello.org
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?