Getting Started with Ruby (scripting language for object-oriented programming)

Source: Internet
Author: User
Tags class definition uppercase letter



Ruby is a scripting language for simple and fast object -Oriented programming (object-oriented programming).



Simple Introduction


  • Ruby is open source and available on the web for free, but requires a license .
  • Ruby is a universal, interpreted programming language .
  • Ruby is a true object-oriented programming language.
  • Ruby is a server-side scripting language similar to Python and Perl.
  • Ruby can be used to write generic Gateway Interface (CGI) scripts.
  • Ruby can be embedded in Hypertext Markup Language (HTML).
  • Ruby syntax is simple, which makes it possible for new developers to learn ruby at high speed and ease.
  • Ruby has a similar syntax to many programming languages such as C + + and Perl.
  • Ruby is extensible, and large programs written in Ruby are easy to maintain.
  • Ruby can be used to develop Internet and Intranet applications.
  • Ruby can be installed in Windows and POSIX environments.
  • Ruby supports many GUI tools, such as TCL/TK, GTK, and OpenGL.
  • Ruby can be very easy to connect to DB2, MySQL, Oracle, and Sybase.
  • Ruby has a rich built-in function that can be used directly from a ruby script.
features


Completely object-oriented, in the Ruby language, no matter what is the object, contains the basic data types in other languages, for example, the integer variable has no type, ruby variables can be saved regardless of the type of data.    No matter what has a value, whether it is a numeric or logical expression or a statement, there is a value.    The Ruby language is so elegant that it can be read without staring. Ruby is a dynamic language, and you can change previously defined classes in your program, and classes that have already been defined can be changed at execution time. It is also possible to define a method specific to that instance in an instance of a class, which is called a singleton method.


Grammar


The keyword in Ruby are as follows:



Modules Definition: module



Class definition: Class



Method definition: Def,undef



Check type: Defined?



Conditional statement: if,then,else,elsif,case,when,unless



Looping statements: For,in,while,until,next,break,do,redo,retry,yield



Logical inference: Not,and,or



Logical value: True,false



Null value: Nil



Exception handling: Rescue,ensure



Object reference: Super,self



Start of block: Begin/end



Embedded module: Begin,end



File Related: __file__,__line__



method returns: Return



Aliases: Alias






Note: The Begin module is equivalent to a macro in the C language, and the end module is used for some finishing work. With require,include, the syntax definition for begin and end should be canceled.






The operators in Ruby include the following: priority (from high to low)



[], []= array subscript, array element assignment  



* * Ride the Underworld



! , ~, +,- non, bit non, unary plus (positive), minus



*,/,% multiply, divide, die



+,- add, subtract



>>, << right shift, left shift



& Bit and



^, | bit XOR, bit, or



<=, <, >, >= less than or equal, less than, greater than, greater than or equal to



<=>, = =, = =, =~,! =,!~ Various equality inferences (cannot rewrite =~,! =,!~)






&& Short Circuit and



|| Short Circuit or



..、... start point of interval to end point



? : ternary conditional operator



=,%=, ~=,/=,-=, + =, |=, &=, >>=, <<=, *=, &&=, | | =, **= various assignments



Defined? Check Type



not logical non-



Or, and logical OR, logical and



If, unless, while, until inference and looping



Begin, end define method, class, scope of module




Attention:

It is important to note that Ruby does not have "+ +", "--" operators, but can be implemented by "+=1", "-=1".

Some conventions for Ruby identifiers:

Local variables start with lowercase letters or underscores

Global variables begin with a dollar sign.

Instance variable starts with @

Class variables start with @@

A constant or class name begins with an uppercase letter.

The Nilkeyword in Ruby are very special. Nil represents a concept of an empty set that is similar to null in other languages. and other languages do not, Ruby in the logical inference process, only nul and false is false, all other expressions are true.






Methods in Ruby



Write amethod in Ruby now and write a simple hello



defSay_hello (name)



result= "Hello," +name



returnresult



End



Puts Say_hello ("oec2003")#返回hello, oec2003



The way to see Ruby is to use the KEYWORDDEF definition followed by the method name, end With end, accustomed to C # and Java, may feel uncomfortable very accustomed. In the inner statement of the method, it is assumed that each statement is placed on a separate line, with no semicolon appended to the statement.



Classes in Ruby



the same classes in Ruby are defined as Keywordclass, followed by theclass name



Class Oec2003



End



Classes typically inherit the base base class in Rails



Class Oec2003<activerecord::base



End



Indicates that Oec2003 inherits the base base class, and the inheritance in Ruby is implemented with <, and the base base class belongs to the module ActiveRecord



Methods in the Ruby class can be added to the access modifier to limit the level of the interview



Class Oec2003



Def method1 #No plus no matter what modifier, feel public

     End

     Protected

     Def method2 #Modifier is proteted, note that the modifier is written on top of the method


 end

    private

    def method3

    end

end








Private



defmethod3



End



End



Modules in Ruby



modules and classes are a bit similar, they all include a set of methods, constants and other classes and module definitions, unlike classes where modules cannot createinstances.



There are two uses for the module. First: The function of the namespace, so that the name of the method will not conflict. Second: The ability to share the same functionality among different classes. Assuming a class is mixed with a module, the class has all of the instance methods in the module, as if it were defined in the class



ModuleOec2003



End



Control structures in Ruby



Conditional Inference statement: Infer whether equality is used = = in the condition, and be careful not to write =



Ifcount>10



puts" countgreater than 10"



elsif count==10#Note here is elsif instead of elseif



puts "Count equals10"



Else



puts "count less than10"



End



Loop statement:



While loop



Whileage<30



putsage



age=+1



End



Single-line while



Age=age+1 while age<30



Until cycle



A=1



Until a>=10



puts a



A+=1



End



For: In.. Cycle



For I in 1..9



puts I,""



End







Getting Started with Ruby (scripting language for object-oriented programming)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.