variable & input/output

Data type

  • Numbers (์ˆซ์ž)

    my_num = 5

  • String (๋ฌธ์ž์—ด)

    my_string = "Ruby"(""์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.)

  • Boolean(true, false)

    my_boolean = true

variable

variable as a word or name that grasps a single value.

my_num, my_string, my_boolean์ด variable์ด๋‹ค.

MATH

  • Addition (+)

  • Subtraction (-)

  • Multiplication (*)

  • Division (/)

  • Exponentiation ( ** )

  • Modulo (%)

puts and print

print์™€ puts์˜ ์ฐจ์ด๋Š” puts ๋Š” ์ž๋™์œผ๋กœ blank line์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 print "Hello"
 puts "Konkuk Likelion 5th"

String Method

Method๋Š” .์„ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค.

  • .length : ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐ.

    "Hello, I am Dahye".length ๊ฒฐ๊ณผ ==> 17

  • .reverse : ๋ฌธ์ž์—ด์„ ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ

    "Konkuk".reverse ๊ฒฐ๊ณผ ==> kuknoK

  • .upcase & .downcase : ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ & ์†Œ๋ฌธ์ž๋กœ

    "eric".upcase ๊ฒฐ๊ณผ ==> ERIC

  • .capitalize : ์ฒซ๋ฒˆ์งธ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž, ๋’ค์˜ ๊ธ€์ž๋Š” ์†Œ๋ฌธ์ž.

    "konkuk".capitalize ๊ฒฐ๊ณผ ==> Konkuk

  • ! method๋์— !๋ฅผ ๋ถ™์ด๊ฒŒ ๋˜๋ฉด ์ˆ˜์ •๋œ๊ฐ’์ด variable์— ์ €์žฅ์ด๋œ๋‹ค.

    a="ruby"
    #=>"ruby"
    a.upcase
    #=>"RUBY"
    a
    #=>"ruby"
    a.upcase!
    #=>"RUBY"
    a
    #=>"RUBY"
  • .include? ์ฃผ๋กœ ์กฐ๊ฑด๋ฌธ๊ณผ ํ•จ๊ป˜ ์“ฐ์ด๋ฉฐ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด true, ์•„๋‹ˆ๋ฉด false

    i="Hello, I am Dahye"
    #=> "Hello, I am Dahye"
    if i.include? "a"
      print "hahaha"
    end
    #=> hahaha=> nil
    i.include? "a"
    #=> true
  • .gsub : global substitution ์œผ๋กœ ๋ฌธ์ž๋ฅผ ๋ฐ”๊พธ๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉ.

    i="Hello, I am Dahye"
    #=> "Hello, I am Dahye"
    i.gsub!(/a/,"e")
    #=> "Hello, I em Dehye"

์ฃผ์„(Comment)

์ฃผ์„์€ ์ฝ”๋“œ๋ฅผ ์งค ๋•Œ ๋„ฃ๋Š” ์„ค๋ช…๋ฌธ์ด๋‹ค. ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์œผ๋กœ ํ•˜์—ฌ๊ธˆ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ ์‹คํ–‰์—๋Š” ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค.

  • #์€ ํ•œ ์ค„์˜ ์ฃผ์„์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

  • =begin =end๋Š” ์ฃผ์„์„ ์—ฌ๋Ÿฌ์ค„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š”๊ฒฝ์šฐ

     =begin
     I'm a comment.
     ์ฃผ์„์„ ์—ฌ๋Ÿฌ์ค„๋กœ ์“ฐ๋Š”๊ฒฝ์šฐ!
     =end

    ```

Getting input

gets๋Š” ๋ฃจ๋น„์˜ method ๋กœ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ(input)์„ ๋ฐ›์•„์˜จ๋‹ค. gets๋Š” ์ž๋™์œผ๋กœ blank line์ด ์ถ”๊ฐ€๋˜๋Š”๋ฐ chomp๋Š” ์ด line์„ ์ œ๊ฑฐํ•œ๋‹ค.

 variable_name=gets.chomp

Printing the Output

gets.chomp๋ฅผ ํ†ตํ•ด ๋ฐ›์•„์˜จ ์ž…๋ ฅ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” #{variable_name}๋ฅผ ํ†ตํ•ด์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 print "What's your first name?"
 first_name=gets.chomp
 print "my first name is #{first_name}"

Last updated