[Ruby]ruby笔记01

  1. hello world

    print("hello world")

  2. 运行

    ruby hello.rb

  3. 注释

    • 单行注释:以#开始,后加注释
    • 多行注释:①使用多个#来注释②写在=begin和=end之间
  4. 字符串长度

    `print(“hello world”.length)

  5. 字符串反转函数

    print("hello world".reverse)

  6. 字符串复制

    print("hello world"*5)

  7. “to” method

    • to_s converts values to strings
    • to_i converts values to Interger
    • to_a converts values to Array

      print(40.reverse) # undefined methodreverse’ for 40:Fixnum (NoMethodError)
      print(40.to_s.reverse)
      `

  8. 创建数组

    []#空数组
    [8,25,59]#创建一个包含有三个整数的数组
    [8,25,59].max#求数组中的最大值,将输出59

  9. 变量

variable,which used to store important data.



         ticket=[11,15,30]  
         print(ticket)
         print(ticket.max)
         print(ticket.length)

10 . 数组排序
使用sort!


        ticket=[10,89,56,13,22,45,65,38,103,89]  
        ticket.sort!

source code:hello.rb

        =begin
            begin和end实现多行注释
        =end
        print("hello world\n")
        print("hello world".reverse)
        print("\n")
        print("hello world".length)
        print("\n")
        print("hello world "*5)

        print("\n"+40.to_s.reverse+"\n")
        ticket=[11,45,39,100,88,102,79,65]

        ticket.sort!
        print(ticket)
        print("\nmax number in ticket:"+ticket.max.to_s)
        print("\n")
        print(ticket.length)

ruby在线学习网站

http://tryruby.org

上一篇:Responsive Web Design


下一篇:面试官:Redis的事务满足原子性吗?