Ruby言語(一)

1735 ワード

#=> Hello World      

       :    print "hello world!"

       :    puts "hello world!"

        :    p "hello world!"

#=> Comment

    # say hello

    =begin
      this is a long comment
    =end

#=> variables

    local: time or _time

    instance: @time

    class: @@time

    global $time

#=> data types

    Numeric

    String

    Symbol

    Boolean

    Array

    Hash

#=> variables tricks

    "hello #{name}"

    a,b = b,a

    3.times{ puts "hello"}

    "hello" * 3


#=> condition if

       if:
	
    if(a>5)
      puts a
    end

       :

    if a > 5 then puts a end

          :    puts a if a > 5

#=> condition unless

     if     unless

    puts "miss it" if !name

    puts "miss it" unless name

         :    a > 5 ? puts(a) : "oh no"

#=> condition if else

    if elsif else:

    if name == "jack"
      "i am rose"
    elsif name == "rose"
      "jack i miss u"
    else
      "get out from here"
    end
   
         switch    :

    case name
    when "jack" then "i am rose"
    when "rose" then "jack i miss u"
    else "get out from here"
    end    
	
#=> loop

         :    3.times{ puts "hello world" }

    for:    
	
	for x in [1,2,3]
      puts x
    end

    while:

    while i > 5 do
      i -= 1
    end
    i -= 1 while i > 5		

    while    until:

    until i <= 5 do
      i -= 1
    end
    i -= 1 until i<= 5		

    while true     :

    loop do
      puts "   "
    end		

           :

    break

    next

    redo

    retry