Ruby Grammar Notes _ruby topics

Source: Internet
Author: User
Tags lowercase

Accept user Input

First_Name = Gets.chomp

Capitalize first letter

first_name.capitalize!

Uppercase letters

first_name.upcase!

The letter becomes lowercase

first_name.downcase!

Multi-line output

Print <<eof
  # Multiline output
EOF

Comments

# I'm a note

Variable fetch

#{first_name}

Variable

    1. Global variable $
    2. Class variable @@
    3. Method variable @
    4. local variable lowercase letter or _

If/else

If a < b
  puts ' 1 '
elsif B < A
  puts ' 2 '
end    

Class

Class Classname
  def functionname (params)
    puts params end Class1

=
classname.new Class1.functionname (' 1 ')
unless
unless false
  print ' OK '
else
  print ' No '
end    

Whether to include characters

print ' puts '

user_input = gets.chomp

user_input.downcase!

If User_input.include? " S "
  print ' has s '
end

Substitution character

# s-> th
user_input.gsub! ( /s/, "th")

To output a variable value in a string

Puts ' Okok #{user_input} '

While

Counter = 1 while

counter < One
  puts counter
  counter = counter + 1
End

Until

Counter = 1
until counter >
  Print counter
  counter = counter + 1
End 
 

+ =, = =, *=,/=
Some languages have the increment operators + + and--(which also add or subtract 1 from a value), but Ruby does not
For loop
# if 1...10 contains 1-9, if 1..10 contains 1-10

For num in 1...10
  puts Num
end  

Loop method
An iterator was just a Ruby method this repeatedly invokes a block of code.

i = loop do I-=
  1
  print "#{i}" break
  if I <= 0
end  

Next

i = loop do I-=
 1
 next if i%2!= 0
 print "#{i}" break
 if I <= 0
end

Array

My_array = [1,2,3,4,5]

The. Each iterator iterator

numbers = [1, 2, 3, 4, 5]

# One way to loop
Numbers.each {|item| puts item}

# Another way to loop
Nu Mbers.each do |item|
 Puts item end


The. Times iterator iterator

10.times {print ' OK '})

Looping with ' while '

num = 1 while

num <= does
  print num =
  1
End  

Looping with ' Until '

num = 1
until num > do
  print num num
  + + 1
end  

Loop the loop with loop

num = 0

loop do
  num + + 1
  print "ruby!"
  break if num = =
  

The. Split method,

Text.split (",")

puts "text to search through:"
text = Gets.chomp
puts "Word to redact"
redact = gets.ch Omp

words = Text.split ("")

Words.each do |word|
  Print Word 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.