How do I rewrite Objective C options bitmasks to to Ruby for RubyMotion?
-
16-06-2021 - |
Domanda
How do I rewrite:
NSUInteger options = kCFCalendarUnitYear | kCFCalendarUnitMonth | kCFCalendarUnitDay;
To Ruby for Rubymotion?
Thanks :)
Soluzione
Almost the exact same way. The only difference really is that since Ruby requires constants to start with a capital letter, you must use a capital K
instead:
options = KCFCalendarUnitYear | KCFCalendarUnitMonth | KCFCalendarUnitDay
# returns 28, same as in Objective-C.
Altri suggerimenti
Dylan Markow's answer is correct. Sometimes you might also want to create a bitmask from an array of values programmatically and you don't have the luxury of using the bitwise or operator directly.
For instance:
options = []
options << KCFCalendarUnitYear
options << KCFCalendarUnitMonth
options << KCFCalendarUnitDay
A nice little trick here is to use inject like so:
options.inject(:|)
It has the added advantage of looking like the face of a monkey.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow