---
# System prepended metadata

title: Ruby 學習心得
tags: [Ruby]

---

Ruby 學習心得
===
###### tags: `Ruby`

參考: [使用VScode调试Rails](https://www.codenong.com/f5236b145aec5055bfb3/)
```shell=
gem install ruby-debug-ide
gem install debase
```

# winget 安裝 ruby
參考: [How to install](https://winget.run/pkg/RubyInstallerTeam/Ruby.3.1)
```bash=
winget install -e --id RubyInstallerTeam.Ruby.3.1
```

# hello world
```ruby=
puts "Hello World!"
```

# launch.json
```
{
    // 使用 IntelliSense 以得知可用的屬性。
    // 暫留以檢視現有屬性的描述。
    // 如需詳細資訊，請瀏覽: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Local File",
            "type": "Ruby",
            "request": "launch",
            "program": "${workspaceRoot}/main.rb"
        }
    ]
}
```

# 錯誤處理
## RSA 加密範例
```ruby=
require "openssl"
require "openssl/oaep"
require "base64"
require "json"

key = OpenSSL::PKey::RSA.new(File.read("public.pem"))

payload = {
  "ts": 1641785544,
  "secureToken": "17919e3e7892d22872ffdaaeac46b138d0f6131e8bc5593aabaa7d89babcab30",
  "action": 2,
  "belongsDC": "FATCAT",
  "username": "api_tester",
  "uid": "DFAT1588073831095e",
  "type": "deposit",
  "amount": 10.0,
}.to_json

label = ""
md = OpenSSL::Digest::SHA1

jsonStringLength = payload.length
partitionLength = 100
startIndex = 0
endIndex = partitionLength

buf = ""

loop do
  if endIndex > jsonStringLength
    endIndex = jsonStringLength
  end
  t = payload[startIndex..endIndex - 1]
  cipher_buffer = key.public_encrypt_oaep(t, label, md)
  buf += cipher_buffer

  if endIndex >= jsonStringLength
    break
  end

  startIndex += partitionLength
  endIndex += partitionLength
end

x = Base64.encode64(buf)

puts x

```
```shell=
Uncaught exception: cannot load such file -- openssl/oaep
```
有套件沒安裝，下面是安裝指令:
```shell=
gem install openss
gem install openssl
gem install openssl-oaep
gem install base64
gem install json
```


## 參考資料
[ruby 台灣官網](https://www.ruby-lang.org/zh_tw/)
[uby初学者教程第2讲：安装Ruby](https://www.youtube.com/watch?v=f7s1df48DJo&t=93s)

