• Gems that come with the default ruby installation. 安裝 Ruby 時就會預設安裝的 Gems。 • Part of the Standard library. Standard library 的 一 部分。 • To provide extended features. 提供額外功能。 • 3 types of Standard libraries. 有 3 種 Standard libraries Default Library, Default Gem & Bundled Gem.
library. Will NOT become Gems. Ruby 版本/實作相關的 library。不會變成 Gems。 • Example 範例 • mkmf: Generate Make fi le for C extensions (only in MRI) 幫 C extension 產 生 Make fi le (只有 MRI 有) • rbcon fi g: List system info like size of Long. (JRuby use FFI) (MRI use c extension) 列舉系統資訊,像是 long 的 大 小 。 (JRuby 用 FFI) (MRI 用 c extension)
https://bugs.ruby-lang.org/issues/9852 2.2.0 開始 • Installed by default. 預設就會安裝 • Can be upgraded & uninstalled. 可以升級或刪除。 • Need to be added to Gem fi le when using Bundler。 用 Bundler 的時候需要加進 Gem fi le 裏。 • Not maintained by Ruby core. 不由 Ruby core 維護。
gems can be uninstalled. Bundled gems 可以被反安裝。 • Bundled gems need to be added to Gem fi le when using Bundler。 bundled gems 在 用 Bundler 的時候需要加進 Gem fi le 裏。 • There's an e ff ort to promote default gem into bundled gem by @hsbt. @hsbt 正在努 力 的把 default gem 升格為 bundled gem。
: • Use Mersenne Twister, need a seed. 用 Mersenne Twister,需要 seed。 • Can be predicted after 624 calls. 觀察輸出 624 次後就可以預測下 一 個值。 • Secure Random • Use /dev/urandom, Win32 or openssl 使 用 /dev/urandom, Win32 or openssl • Hard to predict next output 很難預測下 一 個值。
require your class to provide 2 methods 是 一 個 mixin module。需要 自己 實作兩個 method。 • tsort_each_node to provoide a list of nodes. 提供所有的 node • tsort_each_child to provide a list of children for a speci fi c node. 提供某 一 個 node 的所有 child。 • Call tsort to get reversed topological sort order. 回傳反轉過的拓墣排序。 require 'tsort' # => true class Hash include TSort alias tsort_each_node each_key def tsort_each_child(node, &block) fetch(node).each(&block) end end # => :tsort_each_child {1=>[2, 3], 2=>[3], 3=>[], 4=>[]}.tsort # => [3, 2, 1, 4]
uniq abbreviation for a set of string. 幫 一 組字串找出所有可能的縮寫。 • Useful for CLI arguments, for example ./my_cmd u can be expanded into ./my_cmd update using abbrev. 處理命令列參數時很好 用 ,./my_cmd update 可以縮寫成 ./my_cmd u。 • Maintainer: Akinori MUSHA (@knu) •
of stings. 輸入是 一 個字串陣列。 • returns a Hash as lookup table. 回傳 一 個Hash 查找表。 • No ambiguous pre fi x in the lookup table. 表裡 面 沒有會對到不同字串的 pre fi x Abbrev.abbrev(%w{ install update upgrade }) # => {"install"=>"install", "instal"=>"install", "insta"=>"install", "inst"=>"install", "ins"=>"install", "in"=>"install", "i"=>"install", "update"=>"update", "updat"=>"update", "upda"=>"update", "upd"=>"update", "upgrade"=>"upgrade", "upgrad"=>"upgrade", "upgra"=>"upgrade", "upgr"=>"upgrade", "upg"=>"upgrade"}
as cli one liner. 提供 一 些命令列的指令。 • Useful in Make fi le for cross platform。 寫跨平台 Make fi le 的時候很好 用 。 • When combined with the require -r fl ag, looks like -run 當和 require 用 的 -r 參數放在 一 起的時候。看起來像 -run。 • Maintainer: WATANABE Hirofumi
-- [OPTION] SOURCE DEST to copy fi les 複製檔案 • ruby -run -e mv -- [OPTION] SOURCE DEST to move fi le/folder 移動檔案 • ruby -run -e httpd -- [OPTION] [DocumentRoot] start a http server at DocumentRoot, but need to install webrick after Ruby 3.0.0. Useful to transfer fi le in local network. 開 一 個 http server。不過在 Ruby 3.0.0 之後需要額外裝 webrick。 用 來在區網傳檔案很好 用 。
Ruby have this as builtin. 大 部分 人 不知道 Ruby 有內建 Or don't know we can use it directly. 或是不知道可以直接拿來 用 。 • prime • Forwardable • Delegate • Singleton • did_you_mean
method calls to another object. 轉發特定 method 給其他物件。 • Maintainer: Keiju Ishitsuka, Ruby' god father. Who coined the name Ruby with Matz Ruby 的教 父 ,和 Matz 一 起討論了 Ruby 這個名字。
all unde fi ned methods to the the target object. 轉發所有未定義的 method 給 目 標物件。 class TimeDecorator < SimpleDelegator def years_ago = Time.now.year - year end # => :years_ago t = TimeDecorator.new(Time.new("1993-02 -24 UTC")) # => 1993-02-24 00:00:00 UTC t.year # => 1993 t.years_ago # => 30
instance. 回傳單例實體。 • .new is marked private. .new 變成 private • Easier to understand than class instance variable @@var. 比起 用 class instance varible 容易理解。 • Thread safe 執 行 序安全。 class BDFL include Singleton def name = "Matz" end # => :name BDFL.instance.name # => "Matz" BDFL.new # => (...:in `<main>': private method `new' called for class BDFL (NoMethodError)
in error messages. 在錯誤訊息中產 生 建議。 • Works for methods & constants etc. 對 method 和 constant 名都有效。 3.3.0-preview3 :001 > evel(1) (irb):1:in `<main>': undefined method `evel' for main (NoMethodError) Did you mean? eval 3.3.0-preview3 :003 > Irb (irb):3:in `<main>': uninitialized constant Irb (NameError) Did you mean? IRB
Tons of good stu ff s in standard libraries. Ruby 內建很多功能。標準函式庫裡有很多好東 西 。 • There's an ongoing e ff ort of promoting standard libraries to bundled gems. 正在努 力 把標準函式庫升級成 bundled gems。 • https://stdgems.org/ have a list of standard libraries. 有標準函式庫的清單。 • Next time you can check the Standard library before searching for 3rd party gems. 下 一 次在 用 第三 方 Gem 之前,可以先看看標準函式庫有沒有。 •