0
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 5 years have passed since last update.

メモ たのしいRubyChapter1

0
Last updated at Posted at 2019-04-14

関数

rubyのメソッドでは()を省略できる。

print

nilはprint文自体の戻り値


print("Hello.\n")
Hello
=>nil

シングルクォートで文字列を囲った場合、「\n」などの特殊文字の解釈を行わず、そのまま表示する。
ただし「\」と「'」を表示したい場合は、その前に「\」をつける。


print 'Hello \n'

文字列を続けて表示したい場合は「,」で区切る


print "Hello,", "Ruby", ".", "\n"

putsメソッド

printメソッドとは異なり、文字列の最後で必ず改行する


puts "Hello, Ruby"
puts "Thank you"

文字列にデータを埋める方法

文字列の中に#{変数名}を入れる


x=10
y=20
z=30
area=10*20+z

print "areaデータ=#{area}\n"

pメソッド

デバッグ用で使われる。特殊文字がそのまま出力されたり数値かどうか識別できたりする。


p "100" #=> "100"
p 100 #=> 100

コメント

  • 一行単位 #
  • 範囲指定 =begin =end

# コメント
=begin
ここはコメント
ここはコメント
ここはコメント
=end

if

a=20
if a>=10
  print "greater\n"
end

b=30
if b>=10
  print "greater\n"
else
  print "smaller\n"
end

while


i=1
while i <=10
  print i, "\n"
  i=i+1
end

times

イテレータメソッド
doは省略できない

i=0
100.times do
  puts i
  i+=1
end
0
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
0
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?