Linux command
ruby 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Start an interactive Ruby session
ruby
Execute a Ruby script
ruby [script.rb]
Execute Ruby code from command line
ruby -e "[puts 'Hello, World!']"
Check syntax without executing
ruby -c [script.rb]
Run with warnings enabled
ruby -w [script.rb]
Execute and print result of expression
ruby -e "p [1, 2, 3].sum"
Process input line by line
ruby -n -e "puts $_.upcase" [file.txt]
In-place edit a file
ruby -i -pe "gsub(/old/, 'new')" [file.txt]
说明
ruby is the interpreter for the Ruby programming language. It executes Ruby scripts or runs interactive sessions, providing a dynamic, object-oriented environment for general-purpose programming. Ruby emphasizes programmer happiness and productivity with elegant syntax. Everything is an object, and the language supports multiple programming paradigms: object-oriented, functional, and imperative. The -e option allows quick one-liners without creating a file. Combined with -n or -p, Ruby becomes a powerful text processing tool similar to awk or perl. For interactive exploration, use irb (Interactive Ruby) or pry for enhanced REPL features.
参数
- -e _command_
- Execute command as one line of script
- -c
- Check syntax only, don't execute
- -w
- Enable warnings
- -W _level_
- Set warning level (0=silent, 1=medium, 2=verbose)
- -d, --debug
- Enable debug mode
- -v, --verbose
- Print version and enable verbose mode
- --version
- Print version and exit
- -n
- Wrap script in while gets() ... end loop
- -p
- Like -n but print $_ after each iteration
- -a
- Auto-split mode (with -n or -p), sets $F
- -i _ext_
- In-place edit mode (backup with extension if given)
- -I _dir_
- Add directory to load path
- -r _library_
- Require library before executing
- -S
- Search for script in PATH
- -x _dir_
- Extract script from message and change to dir
FAQ
What is the ruby command used for?
ruby is the interpreter for the Ruby programming language. It executes Ruby scripts or runs interactive sessions, providing a dynamic, object-oriented environment for general-purpose programming. Ruby emphasizes programmer happiness and productivity with elegant syntax. Everything is an object, and the language supports multiple programming paradigms: object-oriented, functional, and imperative. The -e option allows quick one-liners without creating a file. Combined with -n or -p, Ruby becomes a powerful text processing tool similar to awk or perl. For interactive exploration, use irb (Interactive Ruby) or pry for enhanced REPL features.
How do I run a basic ruby example?
Run `ruby` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -e _command_ do in ruby?
Execute command as one line of script