Ruby 学习笔记
安装
macOS:
brew install ruby
Ubuntu:
sudo apt install ruby
包管理器
Gem 是 Ruby 编程语言的包管理工具,类似于 Python 的 pip 或者 JavaScript 的 npm。
基本命令
gem install <gem_name> # 安装 Gem 包
gem list # 列出已安装的 Gem 包
gem search <gem_name> # 搜索 Gem 包
gem update <gem_name> # 更新 Gem 包
gem uninstall <gem_name> # 卸载 Gem 包
gem info <gem_name> # 查看 Gem 包信息
查看 Gem 包文档:
gem server
运行此命令后,打开浏览器并访问 http://localhost:8808,可以查看本地已安装 Gem 包的文档。
Bundler
在实际项目中,我们通常使用 Bundler 来管理 Gem 依赖项。Bundler 使用 Gemfile 文件来定义项目所需的 Gem 以及它们的版本。
安装 Bundler:
gem install bundler
在项目根目录下创建一个名为 Gemfile 的文件,内容如下:
source 'https://rubygems.org'
gem 'rails', '~> 6.1.0'
gem 'pg', '>= 0.18', '< 2.0'
安装 Gemfile 中指定的 Gem:
bundle install
更新 Gemfile 中指定的 Gem:
bundle update
版本管理器
Ruby 的主流版本管理器有 RVM 和 rbenv。我还没有遇到需要使用版本管理器的情况,因此这里留空。
基本语法
变量
name = "Alice"
age = 30
puts "Name: #{name}, Age: #{age}"
将代码保存为 hello.rb。然后运行:
ruby hello.rb
变量作用域:
var # 局部变量
@var # 实例变量
$var # 全局变量
数据类型
Ruby 支持多种数据类型,包括字符串、数字、数组、哈希等。
str = "Hello, Ruby!" # 字符串
num = 42 # 数字
arr = [1, 2, 3, 4, 5] # 数组
hash = {name: "Alice", age: 30} # 哈希
条件语句
age = 18
if age < 18
puts "You are a minor."
elsif age >= 18 && age < 65
puts "You are an adult."
else
puts "You are a senior."
end
循环
while 循环:
i = 0
while i < 5
puts "i is #{i}"
i += 1
end
each 循环:
arr = [1, 2, 3, 4, 5]
arr.each do |num|
puts num
end
方法
def greet(name)
return "Hello, #{name}!"
end
puts greet("Alice")
类和对象
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def introduce
"Hello, my name is #{@name} and I am #{@age} years old."
end
end
person = Person.new("Alice", 30)
puts person.introduce
Homebrew Ruby 脚本
Homebrew 使用 Ruby 脚本(称为 Formula)来定义每个软件包的安装过程。一个 Formula 文件通常包含了软件包的名称、版本、来源、依赖关系以及安装方法等信息。
Formula
class Speedtest < Formula
desc "Ookla Speedtest"
homepage "https://speedtest.net/apps/cli"
url "https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-macosx-universal.tgz"
sha256 "c9f8192149ebc88f8699998cecab1ce144144045907ece6f53cf50877f4de66f"
version "1.2.0"
def install
bin.install "./speedtest"
man.mkpath
man5.install "./speedtest.5"
end
test do
assert_match /\b1\.2\.0\b/, shell_output("#{bin}/speedtest --version")
end
end
Cask
# 定义一个 cask 包,名称为 "steam"
cask "steam" do
version "4.0" # 包版本为 4.0
sha256 :no_check # 不检查下载文件的 SHA-256 校验和
# 指定包下载 URL,并验证来源域名
url "https://cdn.cloudflare.steamstatic.com/client/installer/steam.dmg",
verified: "cdn.cloudflare.steamstatic.com/"
name "Steam" # 包名称
desc "Video game digital distribution service" # 包描述
homepage "https://store.steampowered.com/about/" # 包主页链接
# 定义包的更新检查策略
livecheck do
url :url # 使用下载 URL 检查更新
strategy :extract_plist # 从 Plist 文件中提取版本信息
end
auto_updates true # 指定包支持自动更新
app "Steam.app" # 指定要安装的应用程序
uninstall launchctl: [ # 定义卸载时要停止的服务
"com.valvesoftware.steam.ipctool",
"com.valvesoftware.steamclean",
],
quit: [ # 定义卸载时要退出的进程
"com.valvesoftware.steam",
"com.valvesoftware.steam.helper",
"com.valvesoftware.steam.helper.EH",
],
delete: "~/Library/Application Support/Steam/Steam.AppBundle" # 指定要删除的应用支持目录
zap trash: [ # 定义清理时要删除的残留数据
"~/Library/Application Support/Steam/",
"~/Library/LaunchAgents/com.valvesoftware.steamclean.plist",
"~/Library/Preferences/com.valvesoftware.steam.helper.plist",
"~/Library/Saved Application State/com.valvesoftware.steam.savedState/",
]
caveats do
requires_rosetta # 提示该应用需要 Rosetta 翻译环境
end
end

浙公网安备 33010602011771号