如何定义生成的用户登录的最小和最大长度
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/