Ruby Learning Day-window Environment building and Ruby Foundation

Source: Internet
Author: User

Learn Ruby Reference tutorial: http://www.w3cschool.cc/ruby/ruby-installation-windows.html

Ruby Learning under the WINDOWX
One. Install ruby, download the latest window stable Ruby, this is 1.9.3 version
Click Next installation until completed, for convenience, there will be a check box below the installation screen to indicate whether the option to install the Watir,devkit and environment configuration, check here, it is not so troublesome configuration.
After the installation is complete, go directly to the command line and enter Ruby-v. If there is no problem, the current Ruby version information should be displayed.

Two. Hello Ruby applet.
Create a. rb file in any directory, where I created a test.rb under the E disk. The content is: print ("hello,ruby!\n") is saved.
In cmd mode to enter E: disk, direct input test.rb Enter, will promise hello,ruby! , the example is complete.

Three. Ruby syntax

Ruby identifiers are case-sensitive. A statement with A; end. The name of a Ruby identifier can contain letters, numbers, and underscore characters
Ruby Stitching object stitching with >> or <<. Ruby's begin statement corresponds to the static code block of Java, and the end statement corresponds to the BEGIN statement.
The end keyword is also used to describe the end of a block of code (class, end of method).

Ruby is an object-oriented language, and the structure of the class is as follows:

Class Vehicle class Vehicle
{
Number No_of_wheels number No_of_wheels
Number Horsepower number horsepower
Characters Type_of_tank Function Speeding
Number capacity print ("I m A Method")
Function Speeding End
{or not with curly braces end
Print ("I m A Method")
}

}

    variables in Ruby class
     Ruby provides four types of variables:
      Local variables: Local variables are variables that are defined in the method. Local variables are not available outside the method. In the following sections, you will see more details about the method. Local variables start with lowercase letters            or _.
      Instance variables: Instance variables can be used across methods in any particular instance or object. This means that instance variables can be changed from object to object. The instance variable places the symbol (           @) before the variable name.
      class variables: Class variables can be used across different objects. A class variable belongs to a class and is a property of a class. The class variable places the symbol (@@) before the variable name.
      Global Variables: Class variables cannot be used across classes. If you want to have a variable that you can use across classes, you need to define a global variable. Global variables always start with a dollar sign ($).
 
    instance
     using the class variable @ @no_of_customers, you can determine the number of objects that are created, which determines the number of customers.
       class Customer
         @ @no_ Of_customers=0
       End

Creating an object in Ruby using the new method
An object is an instance of a class. Now you will learn how to create objects of classes in Ruby. In Ruby, you can use the method of the class new to create an object.
Method New is a unique method that is predefined in the Ruby library. The new method belongs to a class method.

The following example creates the two objects of the class Customer Cust1 and Cust2:
Cust1 = Customer. New
Cust2 = Customer. New
Here, Cust1 and Cust2 are the names of two objects. The object name is followed by an equal sign (=), followed by the class name, and then the dot operator and the keyword new.

Custom methods to create Ruby objects
You can pass parameters to method new, which can be used to initialize class variables.
When you want to declare a new method with parameters, you need to declare the method initialize while creating the class.
The Initialize method is a special type of method that will be executed when the new method of a class with parameters is called. (Similar to the construction method in Java)

The following example creates the Initialize method:
Class Customer
@ @no_of_customers =0//equivalent to Java's static object
DEF initialize (ID, name, addr)
@cust_id =id//Assign the local variable ID to the member variable cust_id (instance variable)
@cust_name =name
@cust_addr =addr
End
End

In this example, you can declare a initialize method with an ID, name, addr as a local variable.
In the Initialize method, the values of these local variables are passed to the instance variables @cust_id, @cust_name, and @cust_addr.
Here, the value of the local variable is passed along with the new method.

Now you can create the object as follows:
Cust1=customer.new ("1", "John", "Wisdom apartments, Ludhiya")
Cust2=customer.new ("2", "Poul", "New Empire Road, Khandala")


member functions in the Ruby class

In Ruby, a function is called a method. Each method in the class is started with the keyword Def, followed by the method name.
Method names always start with lowercase letters. In Ruby, you can use the keyword end to end a method.

The following example defines a Ruby method:
Class Sample
def function
Method Body part
End
End

An example of a complete class--"Object--" method invocation:

The following instance creates an object of class Sample and calls the Hello method:

#!/usr/bin/ruby
Class Sample
def hello
Puts "Hello ruby!"
End
End

Use the class above to create an object
Object = Sample. New
Object.hello

This will produce the following results:
Hello ruby!

This article is from the "re-learn Java" blog, please be sure to keep this source http://3131854.blog.51cto.com/3121854/1545406

Ruby Learning Day-window Environment building and Ruby Foundation

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.