像 Lua 那样引用 Hash 元素
galeki
posted @ 2008年7月03日 18:45
in RoR
with tags
ruby hash lua
, 4854 阅读
Lua 中的 Table 可以拿来当作多种数据格式来使用,比如 Hash ( Dictionary ):
-
h = {a=1,b=2,c=3}
-
print(h['a'])
-
1
最爽的是,可以用 h.a 这种形式来引用 Hash 中的元素:
-
print(h.a)
-
1
利用 Ruby 的 method_missing,也可以这样爽一把,仅仅需要:
-
class Hash
-
def method_missing(method, *args)
-
self[method]
-
end
-
end
即可,测试:
-
h = {:a => 1, :b => 2, :c => 3}
-
puts h.a
-
1
只要 hash 的 key 不要与 Hash 内置的方法重名就可以了~
啥?还要实现赋值? 这个好办,只要将上面的代码改为:
-
class Hash
-
def method_missing(method, *args)
-
if method.to_s =~ /=$/
-
self[method.to_s.chop.to_sym] = *args
-
else
-
self[method]
-
end
-
end
-
end
就可以了,测试:
-
h = {:a => 1, :b => 2}
-
h.a = 456
-
puts h.a
-
456
感觉不错~
2022年12月05日 00:09
Aw, this is an incredibly nice post. In idea I must put in writing similar to this additionally – taking time and actual effort to make a top notch article… but exactly what do I say… I procrastinate alot and by no means appear to get something completed. 메이저사이트
================
Are you psychic? You have to be as your writing so perfectly fits the questions I possess in mind on this topic. I only entered a few terms in the search results and I’m very thankful that your article jumped up. 二手Macbook
2023年1月12日 17:27
Hey, you used to write excellent, but the last few posts have been kinda boring¡K I miss your super writings. Past few posts are just a little out of track! come on! 온라인바카라
2023年2月19日 15:20
what we need, so you try to fly I could love me or not, but all I have to wake up to face another tomorrow morning, day, you could 슈어맨
2023年8月23日 21:29
Awsome site! I am loving it!! Will be back later to read some more. I am taking your feeds also 插花課程
2024年1月01日 02:27
I am frequently to blogging we truly appreciate your content regularly. The content has really peaks my interest. I’m going to bookmark your blog and maintain checking choosing info. DESALINATION PLANT MANUFACTURER
2024年1月03日 02:27
There couple of fascinating points at some point in this posting but I don’t determine if these people center to heart. There is some validity but Let me take hold opinion until I check into it further. Great write-up , thanks and then we want more! Combined with FeedBurner in addition swimming pool contractors in Pigeon Forge
====================
Thanks for such a good information, i have bookmarked your site and will visit again. Knoxville web design
====================
This site is really a walk-through it really is the details you wanted concerning this and didn’t know who to inquire about. Glimpse here, and you’ll absolutely discover it. black soap
====================
What’s Going down i’m new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I’m hoping to give a contribution & help different users like its aided me. Good job. Horseback riding Smoky Mountains
2024年1月09日 06:00
This is very interesting, You are a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your great post. Also, I’ve shared your web site in my social networks! casa de cambio bogota
============================================
I rattling glad to find this website on bing, just what I was looking for as well saved to fav. fisio
2024年1月09日 19:16
I went over this internet site and I think you have a lot of fantastic info . permis de construire architecte
==============================
Everyone loves your site.. great colorations & theme. Would anyone style and design this website yourself as well as does you actually hire an attorney to make it happen for you personally? Plz reply as I!|m seeking to style my own, personal weblog and would wish to know where ough obtained that out of. many thanks big fat cc
2024年1月19日 01:03
hello!,I like your writing so much! share we communicate more approximately your post on AOL? I need a specialist in this house to solve my problem. May be that is you! Taking a look forward to see you. music for hookah bars and tea houses
=====================
You actually make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complicated and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it! PFAS CONTAMINATED WATER TREATMENT TECHNIQUES to remove per- and poly-fluoroalkyl compounds (PFAS)
2024年1月22日 22:06
Nice post. I discover some thing more challenging on diverse blogs everyday. Most commonly it is stimulating to read content from other writers and use a little something from their site. I’d prefer to use some while using content on my own weblog regardless of whether you do not mind. Natually I’ll provide link on your internet weblog. Many thanks sharing. Rappelz private server
=========================
whoah this blog is great i like reading your articles. Keep up the great paintings! You know, a lot of individuals are looking around for this info, you can aid them greatly. Media files
2024年1月31日 05:48
I am typically to blogging we actually appreciate your site content. This article has truly peaks my interest. I am about to bookmark your site and keep checking for brand spanking new information. car showsI?m no longer positive where you’re getting your information, however great topic. I must spend a while learning much more or working out more. Thanks for great info I used to be on the lookout for this information for my mission. hotels near meI got what you intend, saved to fav, very decent website . coffee beansGreat Post! I look forward to seeing more from you in the future. There are some very great ideas above. cheap car tyres Birmingham
2024年2月09日 21:28
if you are not eating much fiber, then you will always get indigestion. so eat lots of dietary fibers’ Centro de Emprego Online
====================
It is appropriate time to make some plans for the future and it’s time to be happy. I have read this post and if I could I desire to suggest you few interesting things or advice. Maybe you could write next articles referring to this article. I desire to read more things about it! masaje tantrico palma
====================
When I originally commented I clicked the -Notify me when new comments are added- checkbox and today when a comment is added I purchase four emails using the same comment. Possibly there is in any manner you possibly can eliminate me from that service? Thanks! control de plagas en Madrid
====================
Howdy, i scan your blog occasionally and that i own a similar one and i was simply wondering if you get plenty of spam comments? If therefore how do you forestall it, any plugin or something you’ll be able to advise? i buy such a lot lately its driving me mad therefore any assistance is very much appreciated. reparation pc boulogne billancourt
2024年2月13日 17:27
I your writing style really loving this site. locksmith near me
=========================
Substantially, the article is in reality the freshest on that noteworthy topic. I concur with your conclusions and definitely will eagerly look forward to your forthcoming updates. Saying thanks definitely will not simply just be sufficient, for the amazing lucidity in your writing. I definitely will ideal away grab your rss feed to stay abreast of any updates. Fabulous work and also much success in your business endeavors! refrigerator repair service
2024年2月25日 23:05
<p>
I am extremely inspired with your writing abilities as smartly with the layout on your blog. Is this a paid subject matter or did you customize it your self? Anyway stay up the excellent quality writing, it’s uncommon to look a great blog like this one nowadays.. <a href="https://nieuwsvandaag.nl">Nieuws Vandaag</a></p>
<p>
</p>
<p>
</p>
<p>
==========================</p>
<p>
</p>
<p>
</p>
<p>
Wonderful goods from you, man. I’ve understand your stuff previous to and you are just extremely fantastic. I really like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and you still take care of to keep it smart. I cant wait to read much more from you. This is really a terrific web site. <a href="https://technieuws.nl">Tech Nieuws</a></p>