为什么传递给方法的字符串文字更改为其中一个参数?

Why is a string literal passed to a method changed to one of the arguments?

我有一个使用 Sinatra 构建的相对标准的 Web 应用程序。该应用程序是 运行 Ruby 2.7.0 Mac OS Catalina。

在其中一个路由中,数据被传递给方法get_activity_status。该方法有两个参数:namestatus_id

我从 Pry 得到以下输出:

From: /Users/nathanielsuchy/Code/activitypub/utils/get_activity_status.rb @ line 4 Object#get_activity_status:

    1: require "pry"
    2: # Generate ActivityPub/Activity Streams Status
    3: def get_activity_status(name, status_id)
 => 4:     binding.pry
    5:     tweet = get_twitter_status("https://twitter.com/#{name}/status/#{status_id}")
    6:     return {
    7:       "@context": [
    8:         "https://www.w3.org/ns/activitystreams",
    9:           {

[1] pry(#<Sinatra::Application>)> pp name
"glaad"
=> "glaad"
[2] pry(#<Sinatra::Application>)> pp status_id
"outbox"
=> "outbox"
[3] pry(#<Sinatra::Application>)> 

主应用是这样的,去掉了不相关的路由:

require 'sinatra'
require 'sqlite3'
require 'http'
require 'json'
require 'openssl'
require 'nokogiri'
require 'httparty'

# Include classes
require './classes/profile.rb'
require './classes/tweet.rb'

# Include utils
require './utils/get_twitter_status.rb'
require './utils/get_twitter_profile.rb'
require './utils/get_activity_status.rb'
require './utils/get_activity_profile.rb'

# Configure URLs
BASE_URL = "http://localhost:4567" # URL should be protocol://hostname:port WITHOUT THE TRAILING SLASH
HOSTNAME = BASE_URL
            .gsub("http://", "")
            .gsub("https://", "")
            .gsub(":4567", "")

# Configure bind and port
set :bind, '0.0.0.0'
set :port, 8000
set :public_folder, File.dirname(__FILE__) + '/static'

# Configure OpenSSL
PUBLIC_KEY = File.read("./keys/public_formatted.pem")
PRIVATE_KEY = OpenSSL::PKey::RSA.new(File.read('./keys/private.pem'))

# Configure SQLite3 Database
$db = SQLite3::Database.new "database.sqlite3" # You can rename this database if you want.

# Configure routes

...

# ActivityPub Profile Outbox
get '/:name/outbox' do
  headers = {
    "Content-Type" => "application/activity+json"
  }

  body = {
    "Id": "#{BASE_URL}/#{params['name']}/outbox",
    "Type": "OrderedCollection",
    "TotalItems": "1",
    "OrderedItems": [
      {
        "Id":     "#{BASE_URL}/#{params['name']}/status/1210253456252055552",
            "Type":   "Create",
            "Actor":  "#{BASE_URL}/#{params['name']}/status/1210253456252055552",
            "To":     ["https://www.w3.org/ns/activitystreams#Public"],
            "Cc":     ["#{BASE_URL}/users/#{params['name']}/followers"],
            "Object": get_activity_status("glaad", "1210253456252055552")
      }
    ] 
  }

  return [200, headers, body]
end

app.rb 以某种方式在 status_id 传递给 get_activity_status 之前更改了它的值。为什么会发生这种情况?

如果需要仔细查看应用程序,可以在 GitHub https://github.com/nsuchy/TwitterPub

上找到

Somehow app.rb is changing the value of status_id before it's passed to get_activity_status. Why is this occurring?

没有。当您点击 /:name/outbox 时,实际上此块称为 https://github.com/nsuchy/TwitterPub/blob/master/app.rb#L152,而不是您可能期望的最后一个。发生这种情况是因为 /:name/:status_id 也与 /:name/outbox 匹配(在这种情况下 status_id 被解析为 outbox)。只需重新排序路由的定义(将静态的放在动态的上面)并且它应该工作...