开发者

Shorten way to write code and respect DRY

开发者 https://www.devze.com 2023-02-17 22:03 出处:网络
I wonder if this code -matches.each do |match| 开发者_运维百科=match.clan_1.name =match.score(clan_1)

I wonder if this code

-matches.each do |match|
    开发者_运维百科=match.clan_1.name
    =match.score(clan_1)
    =match.clan_2.name
    =match.score(clan_2)

can become something like this:

-matches.each do
    =clan_1.name
    =score(clan_1)
    =clan_2.name
    =score(clan_2)

I think it will be much more DRY. Any ideas?


The answer is:

-matches.each do |match|
    -match.instance_eval do
        =clan_1.name
        =score(clan_1)
        =clan_2.name
        =score(clan_2)


The best way to DRY this up would be to stick it in a helper method and call that from your views.

0

精彩评论

暂无评论...
验证码 换一张
取 消