在Ruby中用于对象数组的`map`函数。

huangapple go评论63阅读模式
英文:

Map function in Ruby for Array of Objects

问题

以下是翻译好的部分:

需要 "bcrypt"

users = [
    { username: "user", password: "qwertyuiop" },
    { username: "user1", password: "qwertyuiop1" },
    { username: "user2", password: "qwertyuiop2" },
    { username: "user3", password: "qwertyuiop3" },
  ]

def create_hash_digest(password)
  BCrypt::Password.create(password)
end
def create_secure_users(list_of_users)
  list_of_users.map { |user_record| user_record[:password] = create_hash_digest(user_record[:password]) }
end

puts create_secure_users(users)

它仅返回了经过哈希处理的密码。

预期返回一个相同对象的数组,但密码已被哈希处理:

users = [
    { username: "user", password: "hashed_password" },
    { username: "user1", password: "hashed_password" },
    { username: "user2", password: "hashed_password" },
    { username: "user3", password: "hashed_password" },
  ]
 # 这里的hashed_password是保存在映射中的哈希密码

我期望得到一个对象数组,其中密码已被替换为哈希密码。

我可以使用each循环来实现这一点,但希望使用map。

英文:

Under is my array of Object
what i wanted to is to encrypt by password using a map

require "bcrypt"

users = [
    { username: "user", password: "qwertyuiop" },
    { username: "user1", password: "qwertyuiop1" },
    { username: "user2", password: "qwertyuiop2" },
    { username: "user3", password: "qwertyuiop3" },
  ]

def create_hash_digest(password)
  BCrypt::Password.create(password)
end
def create_secure_users(list_of_users)
  list_of_users.map { |user_record| user_record[:password] = create_hash_digest(user_record[:password]) }
end

puts create_secure_users(users)

it is returning just the hashed password

Expected to return an array of same objects but with hashed passwords

users = [
    { username: "user", password: "hashed_password" },
    { username: "user1", password: "hashed_password" },
    { username: "user2", password: "hashed_password" },
    { username: "user3", password: "hashed_password" },
  ]
 #here hashed_password is hashed password that is saved in map

what I am expecting to get array of object where password is replaced with hashed password

I can achieve this with each loop but want to go with map

答案1

得分: 3

map函数中,返回值是最终的操作:

def create_secure_users(list_of_users)
  list_of_users.map do |user_record|
    { username: user_record[:username], password: create_hash_digest(user_record[:password]) }
  end
end

或者

def create_secure_users(list_of_users)
  list_of_users.map do |user_record|
    user_record.merge(password: create_hash_digest(user_record[:password]))
  end
end

两者做的是相同的事情
英文:

In map function the return value is final the operation,

def create_secure_users(list_of_users)
   list_of_users.map do |user_record|
    { username: user_record[:username], password: create_hash_digest(user_record[:password]) }
   end
end

or

def create_secure_users(list_of_users)
  list_of_users.map do |user_record|
    user_record.merge(password: create_hash_digest(user_record[:password]))
  end
end

do the same thing

答案2

得分: 0

感谢 @safak 和 @DaveNewton 提供了两种不同的方法:

使用 merge:

def create_secure_users(list_of_users)
  list_of_users.map { |user_record| user_record.merge(password: create_hash_digest(user_record[:password])) }
end

通过在 map 中返回元素:

def create_secure_users(list_of_users)
  list_of_users.map { |user_record|
    user_record[:password] = create_hash_digest(user_record[:password])
    user_record
  }
end
英文:

Thanks to @safak and @DaveNewton drived 2 different approaches
using merge

def create_secure_users(list_of_users)
  list_of_users.map { |user_record| user_record.merge(password: create_hash_digest(user_record[:password])) }
end

and by returning element in map

def create_secure_users(list_of_users)
  list_of_users.map { |user_record|
    user_record[:password] = create_hash_digest(user_record[:password])
    user_record
  }
end

huangapple
  • 本文由 发表于 2023年6月21日 23:12:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定