On the basic grammar of Ruby _ruby special topics

Source: Internet
Author: User
Tags naming convention uppercase letter

There are several ways to create string objects, most commonly using string literals (literals), a sequence of characters between a set of single or double quotes. The difference between the two forms is that when you construct a literal, Ruby differs in how much the string handles. Ruby has little to do with single quote strings. Except for the few exceptions. The value of this string is formed by typing into the literal amount of the string.

Ruby has more processing for double quote strings. First, it looks for a sequence that starts with a backslash and replaces them with a binary value. The most common one is \ n, which is replaced by a carriage return line feed. When a string output that contains a carriage return line break is enforced, \ n will force a newline.

Puts "and good night, \NGRANDMA"

Output results:

And good night,

grandma


The second thing Ruby does with a double quote string is an expression interpolation within the string (expression interpolation), and the #{expression} sequence is replaced by the value of the expression. You can rewrite the previous method in this way.

def say_goodnight (name) result

 = "good night,#{name}" return result end

puts Say_goodnight (' Pa ')

Output results:

 Good night, Pa

When Ruby constructs this string object, it finds the current value of name and replaces it with a string. Any complex expression is allowed to be placed in the #{...} Structure. Here, the capitalize method defined in all strings is invoked, and the first letter of the parameter is capitalized after the output.

def say_goodnight (name) result

 = ' good night,#{name.capitalize} ' return to result

puts Say_ Goodnight (' Uncle ')

Output results:

Good night, uncle

For convenience, you do not need to provide curly braces if the expression is just a global instance or a class variable.

$greeting = "Hello"   # $greeting is a global variable

@name = "Prudence"  # @name is an instance variable

puts "# $greeting, # @name

Output results:

Hello,prudence

This method can be further simplified. The value returned by the Ruby method is the value of the last evaluated expression, so you can remove both the temporary variable and the return statement.

def say_goodnight (name)

 "Good Night,#{name}"

end

puts Say_goodnight (' Ma ')

Output results:

Good night, Ma

Ruby uses a naming convention to differentiate the use of names: the first character of the name shows how the name is used. Local variables, method parameters, and method names must start with lowercase letters or underscores. Global variables are prefixed by the dollar sign ($), and the instance variable begins with the "at" (@) symbol. The class variable starts with a two "at" (@@) symbol. Finally, class names, module names, and constants must start with an uppercase letter.

Starting with the initial character specified above, the name can be any combination of letters, numbers, and underscores (but the symbol following the @ symbol cannot be a number). However, by convention, instance variable names that contain multiple words use an underscore connection between words, and class variable names that contain multiple words use mixed capitalization (each single initial capitalization). Method name can? 、! and = Character end.

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.