开发者

10K, 20M, 21V kudos etc... in Ruby on Rails

开发者 https://www.devze.com 2023-01-04 05:42 出处:网络
I am creating a snippet-site where people can rate snippets, just like votes on SO. Currently, when a s开发者_运维问答nippet has over 999 kudos, it looks like on this mug:

I am creating a snippet-site where people can rate snippets, just like votes on SO. Currently, when a s开发者_运维问答nippet has over 999 kudos, it looks like on this mug:

10K, 20M, 21V kudos etc... in Ruby on Rails

(source: picocool.com)

So what I want is to do a 1K, 1M, etc... kudos like on SO:

-----
|999|
-----

**user does +1**
-----
|1K |
-----

**one million kudos**
-----
|1M |
-----

The same goes for billions, trillions, quadrillions, quintillions, etc... :'D

Has Ruby got methods for this, and if not, how can I write them myself? Thanks.


I don't think Ruby has something like this. Rails does have a set of number helpers, but doesn't include this specific feature. However, if you view source for "number to human size" you should be able to roll your own without much trouble.


Looks like it has been implemented. I suggest you have a look at this patch:

https://rails.lighthouseapp.com/projects/8994/tickets/4239-patch-improvements-in-number_helperrb


A possible solution is to create an application-wide helper (application_helper.rb inside app/helpers). You can expand this by using nested if's, but I think you get the idea. I am not saying this is the way to do it, but it beats having something working, than nothing at all.

def kudoify(kudos)

if kudos > 1000 && kudos < 1000000 then 
mykudos_prefix = (kudos / 1000 )
mykudos_suffix = (kudos % 1000 )
mykudos = mykudos_prefix.to_s + "K" + mykudos_suffix.to_s
end

return mykudos
end
0

精彩评论

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