Ruby has many features and tags: object-oriented, scripting, cross-platform, open source, and what fascinates me most is that Ruby sees itself as a programming language designed to make programming fun for people. It’s called “happy programming.” Out of interest, I have contacted The language of Ruby, but since I don’t often use Ruby at present, I make an archive of Cheat Sheet here for easy reference later.

The following content should be mostly from the book “Ruby Foundation Tutorial”, which can be regarded as the book’s reading notes. The book is secondhand to buy, but new very, discover as expected still the paper book has the simple sense some 🤔.

At the beginning of Ruby experience

“1” Ruby

There are two ways to execute Ruby commands:

# Execute Ruby files
> ruby helloruby.rb

# Use irB (better for simple quizzes)
> irb
Copy the code

\ is a translation character, and the program does something special for the character after \ in the string, but not for the character in ” :

print("Hello,\nRuby\n! \n")
# Hello,
# Ruby
#!
# => nil

print("Hello \\ Ruby!")
# Hello \ Ruby! => nil

print('Hello,\nRuby\n! \n')
# Hello,\nRuby\n! \n => nil
Copy the code

The exception is if you want to insert \ or ‘in’, you still need to add \ before ‘.

There are many output methods in Ruby:

print "100"  # = > 100
put   "100"  #=> 100\n
p     "100"  # = > "100"
Copy the code

When using the p method, the contents of the string are not translated. In principle, the P method is intended for programmers.

There is also a PP method, called Pretty Print, that prints out nested information more easily.

How to print a variable:

print "Surface area =", area, "\n"
# or
puts "Surface area ="#{area}"
Copy the code

Comments and multi-line comments in Ruby:

=begin multi-line comments created on February 18, 2021 =end
Single line comments
Copy the code

Simple conditions and loops:

# if
a = 20
if a >= 10 then  # then can be omitted
  print "greater\n"
else
  print "smaller\n"
end

# while
i = 1
while 1< =10 do  # do can be omitted
  puts i
  i = i + 1
end
  
# times
100.times do
  puts "Hello, Ruby!"
end
Copy the code

“2” convenient object

Simple use of arrays:

name = ["M"."i"."m"."0"."s"."a"]
name[1]  #=> "i"
name[3] = "o"
name[7] = "?"
puts name
# ["M", "i", "m", "o", "s", "a", nil, "?"]
name.each do |char|
  puts char
end
Copy the code

What is the symbol for?

A symbol is similar to a string. It is usually used as a name tag, such as a hash key, to simplify the symbol as a lightweight string and to convert between strings.

Simple use of hashes:

person = {name: "Mim0sa".nickName: "Mimoku"}
person = {:name=>"Mim0sa".:nickName=>"Mimoku"}
person[:nickName] = "Mim0ku"
person.each do |key, value|
  puts "#{key}: #{value}"
end
Copy the code

Simple use of pattern matching:

/Ruby/  # is a pattern
/Ruby/ =~ "Hi Ruby"  # = > 3
/Ruby/ =~ "Diamond"  #=> nil
/Ruby/ =~ "HI RUBY"  #=> nil
/Ruby/i =~ "HIRUBY"  # = > 2
Copy the code

3 Create command

How to enter data:

puts "The first parameter:#{ARGV[0]}"
puts "The second parameter:#{ARGV[1]}"
puts "The third parameter:#{ARGV[2]}"
> ruby RubyDemo.rb 1 2 3
# First argument: x
# Second parameter: y
# Third argument: z
Copy the code

How to read a file:

filename = ARGV[0]
file = File.open(filename)
text = file.read
print text
file.close
# or
print File.read(ARGV[0])
Copy the code

Use pattern matching to match the content line by line and output:

pattern = Regexp.new(ARGV[0])
filename = ARGV[1]

file = File.open(filename)
file.each_line do |line|
  if pattern =~ line
    print line
  end
end
file.close
Copy the code

Define a method and reference it from another file:

# hello.rb
def hello
  puts "Hello, Ruby!"
end

# use_hello.rb
require_relative "hello"

hello()
Copy the code