variable & input/output
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
Numbers (์ซ์)
my_num = 5
String (๋ฌธ์์ด)
my_string = "Ruby"
(""์ ํด์ค์ผํ๋ค.)
Boolean(true, false)
my_boolean = true
variable as a word or name that grasps a single value.
my_num
,my_string
,my_boolean
์ด variable์ด๋ค.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Exponentiation ( ** )
Modulo (%)
print์ puts์ ์ฐจ์ด๋ puts ๋ ์๋์ผ๋ก blank line์ ์ถ๊ฐํ๋ค.
print "Hello"
puts "Konkuk Likelion 5th"
Method๋ .
์ ์ด์ฉํด์ ์ฌ์ฉ๋๋ค.
.length
: ๋ฌธ์์ด์ ๊ธธ์ด๋ฅผ ๊ณ์ฐ.
"Hello, I am Dahye".length
๊ฒฐ๊ณผ ==> 17
.reverse
: ๋ฌธ์์ด์ ์ญ์์ผ๋ก ์ถ๋ ฅ
"Konkuk".reverse
๊ฒฐ๊ณผ ==> kuknoK
.upcase
& .downcase
: ๋ฌธ์์ด์ ๋๋ฌธ์๋ก & ์๋ฌธ์๋ก
"eric".upcase
๊ฒฐ๊ณผ ==> ERIC
.capitalize
: ์ฒซ๋ฒ์งธ๊ธ์๋ง ๋๋ฌธ์, ๋ค์ ๊ธ์๋ ์๋ฌธ์.
"konkuk".capitalize
๊ฒฐ๊ณผ ==> Konkuk
!
method๋์ !๋ฅผ ๋ถ์ด๊ฒ ๋๋ฉด ์์ ๋๊ฐ์ด variable์ ์ ์ฅ์ด๋๋ค.
.include?
์ฃผ๋ก ์กฐ๊ฑด๋ฌธ๊ณผ ํจ๊ป ์ฐ์ด๋ฉฐ ํฌํจํ๊ณ ์์ผ๋ฉด true, ์๋๋ฉด false
.gsub
: global substitution ์ผ๋ก ๋ฌธ์๋ฅผ ๋ฐ๊พธ๊ณ ์ถ์๋ ์ฌ์ฉ.
#
์ ํ ์ค์ ์ฃผ์์ผ๋ก ์ฌ์ฉ๋๋ค.
=begin =end
๋ ์ฃผ์์ ์ฌ๋ฌ์ค ์ฌ์ฉํด์ผํ๋๊ฒฝ์ฐ
=begin
I'm a comment.
์ฃผ์์ ์ฌ๋ฌ์ค๋ก ์ฐ๋๊ฒฝ์ฐ!
=end
```
gets
๋ ๋ฃจ๋น์ method ๋ก ์ฌ์ฉ์์ ์
๋ ฅ(input)์ ๋ฐ์์จ๋ค. gets๋ ์๋์ผ๋ก blank line์ด ์ถ๊ฐ๋๋๋ฐ chomp
๋ ์ด line์ ์ ๊ฑฐํ๋ค.
variable_name=gets.chomp
gets.chomp
๋ฅผ ํตํด ๋ฐ์์จ ์
๋ ฅ์ ์ถ๋ ฅํ๊ธฐ ์ํด์๋ #{variable_name}
๋ฅผ ํตํด์ ์ถ๋ ฅํ ์ ์๋ค.
print "What's your first name?"
first_name=gets.chomp
print "my first name is #{first_name}"
a="ruby"
#=>"ruby"
a.upcase
#=>"RUBY"
a
#=>"ruby"
a.upcase!
#=>"RUBY"
a
#=>"RUBY"
i="Hello, I am Dahye"
#=> "Hello, I am Dahye"
if i.include? "a"
print "hahaha"
end
#=> hahaha=> nil
i.include? "a"
#=> true
i="Hello, I am Dahye"
#=> "Hello, I am Dahye"
i.gsub!(/a/,"e")
#=> "Hello, I em Dehye"