Accept user Input
Capitalize first letter
Uppercase letters
The letter becomes lowercase
Multi-line output
Print <<eof
# Multiline output
EOF
Comments
Variable fetch
Variable
- Global variable $
- Class variable @@
- Method variable @
- 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
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
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