Ruby入门

简介:
一、 Ruby  Introduction:
  Ruby is "an interpreted scripting language for quick and easy object-oriented programming"。
   1. Interpreted scripting language
    • ability to make operating system calls directly
    • powerful string operations and regular expressions
    • immediate feedback during development
   2. Quick and easy
    • variable declarations are unnecessary
    • variables are not typed
    • memory management is automatic
   3.Object oriented programming
    • everything is an object

 

二、Variables & Constant : 

  1.Variables

  • Local variables:
    –the scope of a local variable is one of proc{ ... }/loop{ ... }.def ... End/class ... End/module ... end
  • Global variables:
    –It can be referred to from anywhere in a program. Before initialization, a global variable has the special value nil.
  • Instance variables:
    –Its scope is confined to whatever object self refers to.
  • Class variables:
    –Same as the static member in C# language.
 
   Note: Don’t use the Ruby keyword when define a variable.

 

   2.Constant   

    Definition:
    • A constant has a name starting with an uppercase character.
    • Constants may be defined within Classes or Module, never in method.
    • It should be assigned a value at most once. 

     e.g.:

      class Demo

          PI=3.1415

         #PI=3.15214     #warning: already initialized constant PI

          def hello

                  PI=3.1415  #wrong

          end

      end

      demo = Demo.new

         demo.hello

 

三、Comment   

    •Single line:

        #(Ctrl + /)

    •Multi-line

       =begin

          …

          …

      =end

 

四、Numeric:

  • Methods:
    • to_f()  #Integer -> Float
    • to_i()  # Float -> Integer, directly delete the part of decimal, if don’t want this, you can use the round method
    • round
    • n.times{|i | …}
    • from.upto(to) {|i| …}
    • from.downto(to){|i| …}
    • from. step(to, step) {|i| …}   
   •Additional:

      a=01123     #0=> Octal

      B=-0x23     #0X=> Hex

      C=+0b1010     #0=> Binary

 

 五、Range:

  1.Definition:
    • val1.. val2  #contains: val1,…, va2
    • val1val2  #contains: val1,…, val2-1 
     E.g.:
      1..5  #contains: 1,2,3,4,5
      1…5  #contains: 1,2,3,4

 

   2.Methods:

    • to_a()                     #convert to array
    • Include?(targetValue)/===(targetValue)     #judge whether contains the targetValue
    • min()/begin()/first()              #Get the minimum value
    • max()/end()/last               #Get the maximum value 
    • reject:   Convert to an array and select some element whichdon’t satisfy the conditional
    • select:   Convert to an array and only select some element whichsatisfy the conditional
    • each:    Iterates over the elements and passing each in turn to the block.

    e.g.      

      a=10..20

      puts a. reject{|x| x<15}

      puts

      puts a. select{|x|x<15}

      puts

      a. each{|x| puts x}

 


本文转自贺满博客园博客,原文链接:http://www.cnblogs.com/puresoul/archive/2012/02/20/2359245.html,如需转载请自行联系原作者。

目录
相关文章
|
算法 Ruby
【Ruby on Rails全栈课程】2.1 ruby语言入门
在第一章中,项目我们已经创建好并且能输出第一个helloworld了,我们先不急着做项目,先了解一下ruby,只是需要看得懂代码。这一章我们简单介绍一些ruby语言的语法
175 0
|
Web App开发 Java iOS开发
Ruby on Rails入门——macOS 下搭建Ruby Rails Web开发环境
这里只介绍具体的过程及遇到的问题和解决方案,有关概念性的知识请参考另一篇:Ruby Rails入门——windows下搭建Ruby Rails Web开发环境 macOS (我的版本是:10.12.3 )下已经自带了 ruby  和 gem ,但 ruby 的版本有点低。
1468 0
|
Ruby Python Linux
Ruby(1):入门
安装:   一般来说linux会自动装ruby,可以通过:  ruby -v 来查看ruby版本   直接使用ruby命令的话,是用来执行ruby文件的。如果要打开交互式ruby解释器。只需要在控制台输入:irb 即可。
899 0
|
数据库 Ruby Java
ruby on rails 入门注意事项
<div class="markdown_views"> <h2 id="ruby-on-rails-入门注意事项">ruby on rails 入门注意事项</h2> <p>首先声明:本文不是ruby on rails的入门教程 <br> 入门教程可以参考: <br><a href="http://www.tutorialspoint.com/ruby-on-rails/r
1753 0
|
Web App开发 Ruby Windows
Ruby Rails入门——windows下搭建Ruby Rails Web开发环境
Ruby on Rails 是一个可以使你开发,部署,维护 web 应用程序变得简单的框架。2015年10月编程语言排行榜 Ruby进入TIOBE排行榜前十,可见其流行程度如此之高。本文就帮你打开Ruby on Rails Web开发的大门,助你进入Ruby on Rails的世界,感受不一样的风景。
1156 0