Recently, I was tasked with creating a calendar which transitioned from month to month such as December to January to February, etc.. Also, I needed to go backward on the transition.
A good way to do this is to use a modulo operator. This means that you can figure out which month is the next month number very easily.
So if the month is December, which is month number 12, to find out which month is next:
month = (month % 12) + 1 or (12 % 12 )+ 1, this will work for any month number from 1 to 12.
BUT to go backwards on the month numbers, here is the operation:
month = ((12+(month-2)) % 12) + 1 ... the funny thing is that this operation does not work in Ruby, but it does in Javascript.
In Ruby, here is the equivalent backward operation:
month = ((month-2) % 12) + 1
What is the difference? Why would there be a difference? I have no idea. It seems like Javascript would generate the right response to -1 % 12, which is -1 because 12 divided by -1 is 0 with -1 left over, but Ruby, Python, Perl, and the Google calculator figure that it should be 11 left over. This may be because a negative remainder would make no sense. So if the modulo is negative, then add it to the second operand to get the real modulo.
Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts
Wednesday, May 19, 2010
Sunday, March 21, 2010
Mac OS X Leopard installing the ruby gem eventmachine
I was having some trouble installing eventmachine (a ruby gem that needs an extension library written in C++) recently in Mac OS X Leopard. I am currently running ruby 1.8.8dev. I think, perhaps, when I compiled ruby from source, the config left out the CXX flags setting. This makes mkmf not know what C++ compiler it needs to set in the Makefiles it creates. Since there are not too many gems that have extensions written in C++ (that I know of), I never really ran into this issue. Here is how to fix this.
Edit this file inside the directory of the gem, in my case;
$ vim /usr/local/lib/ruby/gem/1.8/gem/eventmachine-0.12.11/ext/extconf.rb
Right above the last line (the last line being a call to create_makefile), insert this line:
CONFIG['CXX'] = g++
then do the same thing for this extconf.rb in the fastfilereader dir (or anywhere else in the ext dir where there might be more nested extconf.rb files.
then run this:
cd /usr/local/lib/ruby/gem/1.8/gem/eventmachine-0.12.11; rake gem:install
this should solve the issue of getting errors like this:
make: I.: Command not found
make: dynamic: Command not found
Edit this file inside the directory of the gem, in my case;
$ vim /usr/local/lib/ruby/gem/1.8/gem/eventmachine-0.12.11/ext/extconf.rb
Right above the last line (the last line being a call to create_makefile), insert this line:
CONFIG['CXX'] = g++
then do the same thing for this extconf.rb in the fastfilereader dir (or anywhere else in the ext dir where there might be more nested extconf.rb files.
then run this:
cd /usr/local/lib/ruby/gem/1.8/gem/eventmachine-0.12.11; rake gem:install
this should solve the issue of getting errors like this:
make: I.: Command not found
make: dynamic: Command not found
Subscribe to:
Posts (Atom)
