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

Rubyの「?」「!」の整理メモ

Posted at

表題の件です。

「!」は破壊的メソッドにする。
「?」は真偽値を返すメソッドにする。

という感じです。

例を↓

まずは「!」破壊的メソッド。

#ファイルへの記述
name="taguchi"
puts name
puts name.upcase
puts name
puts name.upcase!
puts name

この場合のコンソールでの出力は

ec2-user:~/environment/ruby_lessons $  ruby hello.rb
taguchi
TAGUCHI
taguchi
TAGUCHI
TAGUCHI

となります。

つづいて「?」真偽値を返すメソッド。

#ファイルへの記述
name="taguchi"
puts name
puts name.empty?
puts name.include?("g")

この場合のコンソールでの出力は

ec2-user:~/environment/ruby_lessons $ ruby hello.rb
taguchi
false
true

となります。

以上、メモです。

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?