gpt4 book ai didi

ruby - 定义最小和最大长度

转载 作者:数据小太阳 更新时间:2023-10-29 08:56:55 24 4
gpt4 key购买 nike

如何定义生成的用户登录的最小和最大长度

FFaker::InternetSE.login_user_name

使用了 gem FFaker

最佳答案

您可以存储 FFaker::InternetSE.login_user_name 的结果,并检查生成的字符串的大小是否在您需要的最小和最大长度之间,如果是,则返回它,否则调用再次运行:

require 'ffaker'

def login_user_name(min, max)
raise 'max can not be minor than min' if min > max
username = FFaker::InternetSE.login_user_name
username.size.between?(min, max) ? username : login_user_name(min, max)
end

p login_user_name(8, 9) # christian
p login_user_name(9, 8) # `login_user_name': max can not be minor than min (RuntimeError)

关于ruby - 定义最小和最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51153689/

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