gpt4 book ai didi

ruby - 如何在 Grape 参数中将 camelcase 更改为 snakecase

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:53 26 4
gpt4 key购买 nike

在我的 Grape 端点中,我有一个名为 user_type 的必需参数。另一方面,我的公共(public) API 将此参数声明为 userType,即驼峰式大小写。有办法改变这个吗?

 module MyAPI
module V1
class SignUp < Grape::API
desc 'Create new user account'
params do
requires :email, type: String
requires :password, type: String
requires :user_type, type: String
end
post :sign_up do
{}
end
end
end
end

最佳答案

这有点难看,但它可以解决问题,因此您可以将它放入方法中。

def sym_to_lower_camel(sym)
sym.to_s.split('_').map{|x| x.capitalize}.join.sub(/^./, sym.to_s[0].downcase)
end

关于ruby - 如何在 Grape 参数中将 camelcase 更改为 snakecase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35436822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com