被 Ruby 耍了一下
Fixnum 有个实例方法 [ ],返回数字二进制表示的第 n 位,比如:
-
> 8[3]
-
1
-
> 10.downto(0){|n| print 255[n]}
-
00011111111
如果传入个符号会怎么样呢? 本以为会出错,发现运行正常,返回 0:
-
> 5[:asdasdasd]
-
0
果不其然,[ ] 对传入的符号做了隐式转换,符号的 to_int 方法返回一个唯一代表本身的整数,一般这个整数很大,也就是返回数字的很高位,自然是 0。
这样的话,像 val[:something] 这样的形式,有可能 val 是个 hash,拿符号当作键,也有可能像上面那样 val 只是个数字。
前两天写个东西,类似下面的代码:
-
@user.data ||= {:a => 1, :b => 2, :xxx => xxx ....... }
-
....
-
val = @user.data[:someting]
运行发现,不管我怎么取,val 的值都是 0 ……百思不得其解 。
后来发现 users 表的 data 这项,不小心在数据库中指定默认值为 0 了,所以 @user.data 就不是 nil,根本没有被赋值为后面的 hash,val 就是 0[:something],怪不得取什么都是 0。
符号可以响应 to_int,字符串无法响应 to_int,所以传入个字符串就报错了,不过数字的 [ ] 方法传入非字符的变量有啥意义呢?检测参数不是 Fixnum 就 raise,这样就好了……
Update: 值得庆幸的是,最新的 Ruby 1.9.0 中已经拿掉了 Symbol 的 to_int 方法~
RUBY_VERSION # => "1.9.0"
:foo.to_int
# ~> -:2: undefined method `to_int' for :foo:Symbol (NoMethodError)
:foo.to_int
# ~> -:2: undefined method `to_int' for :foo:Symbol (NoMethodError)
2020年10月07日 19:42
Lucky program and all plan is filled for the joy for all issues. The paths of the field and essaywriter.org/biology-paper-writing-service are encircled for the toys for all joys. Client is invited for the proper discussion for the goals.
2022年5月28日 19:58
Welcome to the party of my life here you will learn everything about me. Tampa Pest Control
2022年5月28日 19:59
On this page you can read my interests, write something special. Orlando Pest Control
2022年5月28日 19:59
Great info! I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have. DeLand Land Clearing
2022年5月28日 22:45
I should say only that its awesome! The blog is informational and always produce amazing things. international video conference
2022年6月02日 03:03
We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work เว็บหวยไม่อั้น
2022年7月16日 14:04
I prefer merely excellent resources - you will see these people in: Pollen Collection Showflat
2022年7月16日 14:05
On this page you can read my interests, write something special. Lentor Modern Showflat
2022年7月18日 16:20
It is quite beneficial, although think about the facts when it reaches this target. clothing
2022年7月24日 01:33
Cool you inscribe, the info is really salubrious further fascinating, I'll give you a connect to my scene. Boston Italian food
2022年7月24日 01:34
Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing... Maine electrician
2022年7月24日 01:35
This is very appealing, however , it is very important that will mouse click on the connection: Haircut
2022年7月24日 01:36
I am always searching online for storys that can accommodate me. There is obviously a multiple to understand about this. I feel you made few salubrious points in Attributes moreover. Detain busy, awesome career! Supima tees
2022年7月30日 12:29
For true fans of this thread I will address is a free online! lsd sheets for sale
2022年8月01日 07:10
I have a mission that I’m just now working on, and I have been at the look out for such information heels
2022年8月04日 22:38
I encourage you to read this text it is fun described ... 유흥알바
2022年8月05日 20:33
You possess lifted an essential offspring..Blesss for using..I would want to study better latest transactions from this blog..preserve posting.. hasta yatağı
2022年8月05日 20:38
Your blogs further more each else volume is so entertaining further serviceable It appoints me befall retreat encore. I will instantly grab your rss feed to stay informed of any updates. hasta yatağı
2022年8月05日 20:43
Listed here you'll learn it is important, them offers the link in an helpful webpage: hasta yatağı
2022年8月07日 13:36
This is helpful, nonetheless it can be crucial so that you can check out the following website: royalgreen condo
2022年8月07日 13:40
Gives you the best website address I know there alone you'll find how easy it is. müll entsorgen berlin
2022年8月07日 13:45
I use basically superior fabrics : you will discover these products by: Puffing Bird
2022年8月09日 22:39
GHANA News ONLINE is a content curation and syndication website to offer news stories across the length and breadth of Ghana. ghananewsonline
2022年8月10日 11:19
Your texts on this subject are correct, see how I wrote this site is really very good. Home Doctor
2022年8月13日 19:43
I exploit solely premium quality products -- you will observe these individuals on: 밤알바
2022年8月23日 05:51
We are tied directly into the sate’s renewal database which allows us to process your request almost instantly Bitmain Antminer T19 for sale
2022年8月24日 21:54
For true fans of this thread I will address is a free online! fluyezcambios
2022年8月26日 01:54
ОТДЫХ ВСЕЙ СЕМЬЕЙ Особое внимание мы уделили детским программам, чтобы Вы могли полноценно расслабиться и насладиться отпуском. Отдых на косе Беляус
2022年8月31日 04:19
I really thank you for the valuable info on this great subject and look forward to more great posts milk frother mixer
2022年9月01日 02:42
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought you have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention. Sutton Bank Routing Number
2022年9月02日 06:34
Thanks for providing recent updates regarding the concern, I look forward to read more. NBA news
2022年9月05日 04:26
What a really awesome post this is. Truly, one of the best posts I've ever witnessed to see in my whole life. Wow, just keep it up. paypal goods and services
2022年9月06日 12:46
I personally use them exclusively high-quality elements : you will notice these folks during: 먹튀
2022年9月10日 08:18
Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job! Canadian Fake Permanent Resident Card
2022年9月11日 04:23
With so many books and articles coming up to give gateway to make-money-online field and confusing reader even more on the actual way of earning money,
2022年10月20日 13:32
It is quite beneficial, although think about the facts when it reaches this target. Sim Đại Phát
2023年3月25日 01:38
These things are very important, good think so - I think so too... pcr based test
2023年4月08日 13:08
Interestingly you write, I will address you'll find exciting and interesting things on similar topics. Psychologist Jobs Australia
2023年4月09日 17:22
Profit primarily prime quality items -- you can understand them all within: Belize Real Estate Listings
2023年4月16日 02:25
These websites are really needed, you can learn a lot. daman game
2023年5月06日 16:22
Customary visits recorded here are the most straightforward technique to value your vitality, which is the reason why I am heading off to the site regular, hunting down new, intriguing data. Many, bless your heart! ivf in turkey
2023年5月08日 12:53 It's extremely pleasant and meanful. it's extremely cool blog. Connecting is exceptionally valuable thing.you have truly helped bunches of individuals who visit blog and give them usefull data. facial tightening
2023年5月08日 13:44
You may comment on the demand plan of the blog. You should visit it's mind boggling. Your blog audit would swell up your visitors. I was to a great degree fulfilled to find this site.I expected to thank you for this unimaginable read!! desktop vaporizer