gpt4 book ai didi

ruby - 如何在 Ruby 中干掉两个函数?

转载 作者:太空宇宙 更新时间:2023-11-03 17:51:03 24 4
gpt4 key购买 nike

有没有办法通过将这两个函数共享的三行移动到另一个函数来DRY up这两个 Ruby 函数?

def format_currency(number)
number_to_currency(number,
:unit => current_user.currency_unit,
:delimiter => current_user.currency_delimiter,
:separator => current_user.currency_separator,
:format => current_user.currency_format
)
end

def format_currency_for_pdf(number, invoice)
number_to_currency(number / invoice.exchange_rate,
:unit => CURRENCIES[invoice.currency]
:delimiter => current_user.currency_delimiter,
:separator => current_user.currency_separator,
:format => current_user.currency_format
)
end

感谢任何帮助?

最佳答案

正如 taro 所暗示的,它将是:

def format_currency(number)
number_to_currency(number,
currency_hash(current_user.currency_unit)
)
end

def format_currency_for_pdf(number, invoice)
number_to_currency(number / invoice.exchange_rate,
currency_hash(CURRENCIES[invoice.currency])
)
end

def currency_hash unit
{
:unit => unit,
:delimiter => current_user.currency_delimiter,
:separator => current_user.currency_separator,
:format => current_user.currency_format
}
end

关于ruby - 如何在 Ruby 中干掉两个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538101/

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