Differences between Ruby and Python
- Python’s parser implementation is more mature and third-party libraries are of high quality. But Ruby package management is simpler and more convenient. Python is equivalent to pinyin, Ruby is equivalent to bilingual, but Ruby syntax tips need to be obtained from the web by installing plug-ins
- Python is widely used. Ruby, on the other hand, is currently mostly confined to the Web and refined projects.
- Python syntax is simple, Ruby is more powerful and flexible
Common commands :** ruby –version:** View the Ruby environment on your computer
Ruby installation
Ruby Version Manager
rvm & rbenv
- RVM & Rbenv is a command line tool that lets you easily install, manage, and use multiple Ruby environments.
- Both tools essentially fiddle with PATH, one before execution and one during execution.
- If you don’t need to maintain a specific version of your Ruby project, just install a newer version of Ruby. Currently ruby3.0 has compatibility issues with many tools
- If you want to learn more, you can start with the RVM management tool
Common commands:
- RVM list know: Current ruby versions that RVM can manage
- RVM list: Displays the ruby versions currently available on your computer
- RVM install ruby-2.7.3: Use RVM to install the specified version of the Ruby environment
- RVM install Ruby –head: Installs the highest version of Ruby currently available for RVM
The gem command
- Like most programming languages, Ruby benefits from a large number of third-party code libraries.
- Most of these code bases are distributed as gems. RubyGems is designed to help create, score
Enjoy and install these code bases.
- gem search -r/-f
- gem install –version
- gem list
Bundler • Bundler tracks and installs specific versions of gems needed to provide a consistent runtime environment for Ruby projects.
- Source ‘rubygems.org’ : specifies the source
- Gem ‘Rails ‘, ‘4.1.0.rc2’ : required library version
- Gem ‘rack – cache’
- Gem ‘nokogiri’, ‘~ > 1.6.1’
RVM will pull the required libraries from the specified source described in the Gemfile, and then generate a gemfile. lock file. This file is used to record the version information for each bundle install
Cocoapods mimics RVM for iOS development
debugging
Ruby -debug- IDE gem – Middleware
Ruby-debug-ide provides an interactive environment. A protocol to establish communication with an IDE such as RubyMine, Visual Studio Code, or Eclipse. “Ruby-debug-ide” redirects commands from the IDE to the debugger. It then returns the answers/events received from the debugger back to the IDE.
Installation method:
gem install ruby-debug-ide
Copy the code
Note that this installation is installed on the specified version of Ruby. All Ruby tools are installed on the Ruby version
Debase – A real debugger
- Debase is a quick implementation of the standard Ruby debug.rb debugger for Ruby 2.0. It is implemented by leveraging the new Ruby TracePoint class. The core components provide support that the front end can build on. It provides breakpoint handling, stack information, and more.
Installation method:
gem install debase
Copy the code
bundle exec
- When you run commands using Bundle Exec, the gems specified in the gemfile file are loaded into your Ruby program by default.
rubocop
- Rubocop is a code style inspector implemented based on the Ruby-style-Guide/Ruby style guide
solargraph
- Solargraph is a language server that provides Intelligent awareness, code completion, and inline documentation for Ruby.
When writing ruby scripts, there will be a prompt, which is not like xcode annotation, you can see the implementation. Ruby prompt is suitable for a gem plug-in installed on the native machine, and then access the web prompt, using solarGraph
Generally complex scripts are written in Ruby.
env
- printf “\n”env”: {\n “PATH”: “$PATH”,\n “GEM_HOME”:
“GEM_HOME \”, \ n \ “GEM_PATH \” : \ “GEM_PATH,” \ n “RUBY_VERSION” : \ “$RUBY_VERSION” \ n \ n \ n}”
- Output environment variables according to VSCode Ruby configuration rules
VSCode plug-in
- Ruby: all plug-ins in vscode do not represent a feature, they represent middleware that communicates with the native Ruby environment
- Solargraph Autocomplete Tip: Tip plugin
- Error Lens: A plug-in to determine if there is a problem with the current code
Debug:
- Create.vscode in the vscode project directory, and create the launch.json file inside
{ // https://github.com/rubyide/vscode-ruby/wiki/2.-Launching-from-VS-Code#available-vs-code-defined-variables "Configurations ": [{"name": "Debug Ruby Code", // Outputs debugging information "showDebuggerOutput": true, // tells VS Code what debugger to run." Type ": "Ruby", // "launch" allows you to launch the provided program directly from VS Code - or "attach"- allows you to attach to a remote debugging session. "Launch ", // rdebug-ide run bundler exec "useBundler": true, // always stop "stopOnEntry": false, // "program" working directory" CWD ": "Program ": "${file}" "program": "${file}" "program": "${file}" // provide program command parameter "args": [], // Before starting the program, provide the environment variable "env" to be set: {"PATH": "/ Users/ws /. RVM/gems/ruby - server/bin: / Users/ws /. RVM/gems/ruby - server @ global/bin: / Users/ws /. RVM/rubieslast/ruby - server/bin: / User S/ws /. RVM/bin: / usr/local/opt/LLVM @ 11 / bin: / usr/local/opt/sqlite/bin: / usr/local/opt/[email protected] / bin: / usr/local/opt/openssl @ 1.1 / bin: / usr/local/opt/ruby/bin: / Users/ws/thirdpartyLib/custom: / Users/ws/yarn/bin: / Users/ws/config/yarn/global/node_m odules/.bin:/Users/ws/thirdpartyLib/jtool/jtool2:/usr/libexec/java_home:/Users/ws/thirdpartyLib/protobuf37/bin:/Users/ws /.pub-cache/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion. The app/Contents/Public/Library/Apple/usr/bin ", "GEM_HOME" : "/ Users/ws /. RVM/gems/ruby - server" and "GEM_PATH" : "/ Users/ws /. RVM/gems/ruby - server: / Users/ws /. RVM/gems/ruby - server @ global", "RUBY_VERSION" : "ruby - server"}}}]Copy the code
The problem is that Ruby plug-ins are re-specified, and there is a problem if the computer’s current Ruby environment doesn’t match the version specified in Configurations. Configurations is the environment variable that runs the current ruby script in vscode, and is automatically generated by the env command above
Symbol
- Ruby is a powerful object-oriented scripting language where everything is an object
- In Ruby Symbol stands for “name”, such as the name of a string, or the name of an identifier.
- Create a Symbol object by prestarting the name or string with a colon:
- In Ruby, every Object has a unique Object Identifier.
- For Symbol objects, a name uniquely identifies a Symbol object. Ruby uses symbols internally all the time, and Ruby also has Symbol tables internally
- A Symbol is essentially a number that forms a one-to-one mapping with the name that created it. The String object is a heavy C struct, so the overhead of using Symbol is quite different from that of String.
- The Symbol table is a global data structure that holds the (numeric ID, name) of all symbols. Ruby does not remove symbols from it, so when you create a Symbol object, it will remain there until the end of the program.
cocoapods-binary
- Cocoapods-binary switches on and off the pod Insatll process to precompile the library, generate the framework, and integrate it into the project automatically.
- The whole precompilation work is divided into three stages:
- Binary Pod installation
- The pre-compilation of binary Pod
- Binary Pod integration