4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

第5回(入出力)

Last updated at Posted at 2020-12-06

puts hello_world

お題

Hello world.

と打ち出すプログラムを作る.

解法

まずはシングルで囲って,

puts 'hello world'

を試す.puts_hello_world.rbに書いて

> ruby puts_hello_world.rb

結果

hello world

類題

pとかprintとかも試す. 結果の違いはコメントに

# print
print 'hello world'
#改行なしで出力

# p
p 'hello world'
#改行されるが出力結果が" "で囲まれる

# pp
pp 'hello world'
#pと同じ

# printf
printf 'hello world'
#printと同じ

getsなんだけど,ARGVで行きます!

お題

> ruby hello_name.rb Rudy

と打ち込んだ時に,

Hello Rudy.

と返ってくるcodeを書け.

簡単な解説

putsしたら普通はgetsだが,今回はARGVというのを使う.

puts ARGV[0]

とhello_name.rbに打ち込んで,

> ruby hello_name.rb Rudy

結果

> Ruby

次に"Hello Rudy"と打ち出させることを考える.

解法

hello_name.rbに次のように書く

puts "Hello #{ARGV[0]}"

同じように実行

> ruby hello_name.rb Rudy

結果

> Hello Ruby

他の方法も試す

#
puts "Hello "+ARGV[0]
#

#
print "Hello #{ARGV[0]}\n"
#

#
print "Hello "+ARGV[0]+"\n"
#

###結果は全てHello Ruby

類題

次に"Hello Rudy."というのをどこかへ保存することを考える.

ruby hello_name.rb bob > hello_name.txt

catで中身確認

cat hello_name.txt

結果

Hello bob

Footnotes:

1

DEFINITION NOT FOUND.


  • source ~/grad_members_20f/members/yoshida/c1_puts.org
4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?