1
1

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

Ruby基礎知識

to_sメソッド

オブジェクトの内容を文字列化するメソッド

ruby

puts 1.to_s + 7.to_s #=> "17"

puts 'nil'.to_s #=> ""

to_iメソッド

文字列表記から整数表記へ変換するメソッド

ruby
str = "23"
puts str.to_i + 12  #=> 35

to_fメソッド

整数表記から小数表記へ変換するメソッド

ruby
n = 5
puts n.to_f       #=>5.0
puts n.to_f / 2   #=>2.5

メソッドの呼び出し

オブジェクト.メソッド(引数)
*()は省略可

ruby
#数値を16進数の文字列に変換

puts 14.to_s(16) #=> "e"

puts 14.to_s 16 #=> "e"

####コメントの入れ方

ruby
# 1行の場合(文の途中からでも可)

=begin
複数行
コメント
可
=end

####文字列に変数を組み込む

ruby
age = 24
puts "私の年齢は" + age + "歳です"    #=>エラー
puts "私の年齢は #{age} 歳です"       #=>私の年齢は24歳です
puts "私の年齢は" + age.to_s + "歳です" #=>私の年齢は24歳です

sprintfメソッド

ruby
sprintf('%0.3.f', 1.2)  #=> "1.200"
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?