Ruby basic syntax rules

Source: Internet
Author: User

1. Common Ruby data types: Numbers, strings, booleans

my_num = 25

my_boollean = true (or false)

my_string = "Ruby"
2. Ruby commonly used mathematical calculation operators

Plus (+)
Less (-)
Multiply (*)
except(/)
Power (**)
Surplus (%)
3.Ruby output operations

puts with line breaks

print without line breaks

Print string: print "HelloWorld" (without line breaks)

Puts "HelloWorld" (line feed)

Print variable: name = "Ruby"

Print "# {name}"
4. String common operation functions

.length (calculate the length of the string)

name = "Ruby"

name.length (return 4 is equivalent to "Ruby" .length)

.reverse (reverse string)

name.reverse (returns ybuR equivalent to "Ruby" .reverse)

.upcase & .downcase (convert case)

name.upcase and name.upcase respectively return RUBY ruby
 

5. Single-line and multi-line comments

Single-line comments begin with # eg. # I ‘m a comment

Multiline comment format is
= begin
I ‘m a comment!
I do n‘t need any # symbols.
= end
Note: There must be no spaces between = and begin and end.
6. Variable naming rules

Variable names generally start with a lowercase letter and are separated by underscores between words, eg. Counter, mastrful_method

Ruby does not prevent you from starting with special symbols such as $, @, etc., but it is best not to do so, it is easy to be ambiguous and reduces readability.
7. Method call

Call method with. Operator

You can call one method at a time or make joint calls

For example: name = "Ruby"

can

Name.downcase

Name.reverse

Name.upcase

It can also be called like name.downcase.reverse.upcase
8. Get input

print "what ‘s your first name?"
first_name = gets.chomp

gets is a method used to get input information, Ruby automatically adds a newline character after it \ n chomp is a method used to delete newline characters.
eg:
Print "What ‘s your first name?"
First_name = gets.chomp
Print "What ‘s your last name?"
Last_name = gets.chomp
Print "What are you from?"
City = gets.chomp
Print "what ‘s your state?"
State = gets.chomp

Print "# {first_name} # {last_name} # {city} # {state}"
9. Ruby program control flow

Conditional judgment sentence if / else

print "Integer please:"
user_num = Integer (gets.chomp)

if user_num <0
  puts "You picked a negative integer!"
elsif user_num> 0
  puts "You picked a positive integer!"
else
  puts "You picked zero!"
end

Pay attention to the end of elsif
unless

if (x <5) then statement1 end

unless x> = 5 then statement1 end
These two sentences are equivalent

if x <5 then statement1
else
statement2
end

unless x <5 then statement2
else
statement1
end
Also peer

unless is used to check whether the following conditions are false, if it is false, the subsequent code is executed, if it is true, else (unless is equivalent to if not)
Comparison operator

Equal ==

Not waiting! =

Greater than>

Greater than or equal to> =

Less than <

Less than or equal to <=
Logical Operators

And && or || NOT!
 

Ruby basic grammar rules

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.