Javascript Exceptions - testing for true - When is undefined actually undefined?

Javascript has to know what 'it' is before Javascript can tell if 'it' is what you want 'it' to be. Example:
if (unreferencedVariableName) { alert('Impossible! an unreferenced variable cannot return true. In fact it cannot return at all!'); }
Result: ReferenceError: unreferencedVariableName is not defined
/* is as true as the unescapable pain and suffering of being an alive human being  */
if (typeof(cowBoySuckMonkey) === 'undefined') { console.log('suck it!') }


/*   these are false - no if-fu for you!   */
if (typeof(cowBoySuckMonkeee) != 'undefined') { console.log('suck it again!') }
if (!typeof(cowBoySuckMonkeee)) { console.log('suck it for real!') }

/* 'undefined is something I guess - just not what I wanted to test against */
if (typeof(cowBoySuckMonke)) { console.log('suck it nut gobbler!') }

var cowboySuckMonkay = '';
if (typeof(cowboySuckMonkay) != 'undefined') { console.log('suck it empty string!') }
/* no joy */
if (typeof(cowboySuckMonkay)) { console.log('suck it empty string, part duex!') }

/* well, I guess an empty string is false - CaulkGobblin! */
if (cowboySuckMonkay) { console.log('suck it empty string, you still not true!') }

if (cowboySuckMonkay == '') { console.log('suck it empty string, now you true!') }
so much to remember…

Posted by jeremy 18/08/2009 at 15h06


comparison of Fixnum with String failed

Where the error occured

Rails 2.1.2 / bundle_fu / Javascript file

Why the fu…?

A javascript files that ends with a line commented out. -1 is being compared to an empty string.

Fix

Add a blank line at the end of the Javscript file(s) that end with commented lines.

Posted by jeremy 15/08/2009 at 07h45


stupid arguments

rspec controller specs: YourClass.should_receive(:find).with() really cares about argument order and does not accept a hash of values where the params[:key] is named like the key in the hash key => value so:
## controller code
def find_my_fucking_shit
  @my_class = MyClass.special_method_in_model(params[:shit], params[:not])
end

## controller spec
# This will fail and you will want to punch a bitch in the nose because you spent two fucking days 
# figuring you were just really goddamn stupid (which may be true) because it should work but it doesn't
it "should find shit" do
  MyClass.should_receive(:special_method_in_model).with({:shit => 'will', :not => 'work'}).and_return(mock_my_shit)
  get 'find_my_fucking_shit',  :shit => 'will', :not => 'work'
end


# This will pass
it "should find shit" do
  MyClass.should_receive(:special_method_in_model).with('will', 'work').and_return(mock_my_shit)
  get 'find_my_fucking_shit',  :shit => 'will', :not => 'work'
end
Yes, I know about hash_including(). For some reason it failed because ":action => '_controller_action_', :only_path => true" was being included in the with() arguments hash and it made the should_receive fail - even though hash_including is supposed to deal with this. It may be a rspec_rails thing. I have not dug into the rspec code very deep to see what I was doing wrong or if it's a bug or if the universe just hates me.

Posted by jeremy 21/01/2009 at 22h44


shelling out to the man

In irb: get a listing of files/sym/hard links in my home directory
# stupid simple shell command - from irb or a script to get file listing
system 'ls -al ~/jeremy '


In ruby script: Return the result of a command line operation. Without back-ticks ruby will just return true or false from the exit of the issued command. [wikipedia backticks - computer related]
# the path to ghostscript on the system without the newline character on the end
gs_path = `which gs`.strip
# use the path to convert a file outside of ruby - will return true or false
system %(#{gs_path} -q -dSAFER -dNOPAUSE -sDEVICE#tiffg4 -sOutputFile#foo.pdf foo.tif -c quit 2>/dev/null)
# if I want the output from the command suppressed so it doesn't spew oodles of info (after I'm sure it works - it will stop useful errors as well)
`#{gs_path} -q -dSAFER -dNOPAUSE -sDEVICE#tiffg4 -sOutputFile#foo.pdf foo.tif -c quit 2>/dev/null`


Ruby Docs have massive amount of info about ERRNO (whatever the fuck it is - i'm guessing errors and such but haven't learned it yet). These error objects matter greatly and I think ruby has them wrapped up in a rubish way, but as I said I'm ignorant at the moment. I guess there is shorthand for error objects: $! - last exception; $@ - backtrace; There are many more of these. (whimper!)

Posted by jeremy 05/01/2009 at 15h03


rails rjs - things i learned the hard way | xi

too many div’s and things go POOF!

When you have a styled div set with an id inside another div on safari at least. I’m going to explore this further, but it seems at this point I’m stacking my nodes too deep, MEANING if a styled div is inside another div that is replaced by an rjs template which has a visual effect to appear the styled stuff inside the div inside the one affected by the javascript response will remain hidden while all the stuff around it will pop into view. Selecting on the screen around the blank areas will reveal the content. Use span’s or class declarations to avoid the same 4 hours of frustration I just had. Of course this could all be wrong and I’m foolishly pointing west when the true way is north. In any case it wasn’t working as expected and now it is so I am of the mind that I solved my problem, be it maybe for only a moment.

remember to float the containers holding the floats

And clear your floats. Boxes are a pain. Tables are ugly. Love thy stacked attributes.

That is all.

Posted by jeremy 10/04/2006 at 16h28


on the process of progress

Essential understanding: In php str_replace and preg_replace do function similarly yet if you want exact matches you have to specify your regexp pattern with serious prejudice lest you match ANY and ALL partial or exact string matches and fubar your sweet logic with redundant nasties. I consider knowing this progress. Next up is understanding the hows and whys of stringing methods in ruby... Feet don't fail me now!

Posted by jeremy 10/03/2006 at 10h11


the teh

For years I thought 'teh' as a spelling for 'the' was some l337 speak hackerism but it dawned on me last night that if you're typing really fast like they teach you in 10th grade you're fingers will already be poised over the top row so slipping the 'e' before the 'h' would be very common if you're thinking faster than your typing. Kind of like how I always type 'of' instead of 'or'. It's a very persistent defect in my self-styled 4-finger hunt and peck mannerisms.

Posted by jeremy 28/02/2006 at 10h54


holy slow sudo port install mod_ruby

Every time I challenge the silicon gods by installing yet another demanding software package on my powerbook G3 I always start by consciously dismissing the age and condition of the laptop. The machine is 6 years old, has 386mb of ram, a 9 gig hard drive, a 500mHz processor, and a first generation airport card. After installing tiger and developer tools there is a little under 2gb of disk space left.

There is a ghost impression of the keyboard etched into the surface of the LCD screen from the oil left on the keys by my fingers. I have dropped, sat on, stepped on, rolled over, spit on, slammed shut, forced open, poked, prodded, wedged, and pried loose this poor machine during the 6 years, 2 SXSW’s, many business trips, not as many vacations, and 6 operating system upgrades of it’s life.

Every time I do something beyond surf the web on grandpa pismo and it doesn’t start smoking or tell me to hold the power button down in 5 different languages I experience a trepid euphoria not unlike that experienced by a man who only hears the metallic click of the firing pin during a game of russian roulette.

Around minute 30 of hour 2 of installing mod_ruby using darwin ports (just to see if I could) I started to get nervous. I was using a wireless connection, the elderly (original) battery was at 50% and sinking and I was having a hard time staying awake. it was 2:30 a.m. which is actually 3:30 am to my internal clock seeing as we’re only a few days in to the daylight savings time switch.

Being the dedicated, task focused person I am I… fell asleep.

I have yet to assess the outcome of this latest haphazard command line escapade.

Posted by jeremy 23/11/2005 at 03h00


learned the hard way | I.

Ceaseless curiosity and always taking 3 lefts to go right. Some things gleamed recently:
  • USE ‘apachectl’ NOT ‘httpd’ to stop apache1.3 and release the port. THEN ’ sudo /opt/local/apache2/bin/apachectl -f /opt/local/apache2/conf/httpd.conf -k start ’ to get apache2 running. Specifying the full paths is how you tell apache2 it’s their turn to bat.
  • apache2’s http.conf default took two days of my life away. Comment out the unique ID module line and blues skies will you see.
  • to use new config use the command above, but replace ‘start’ with ‘graceful’
  • alias and directory settings are a bitch.
Just a few of the many things I’ve learned. This knowledge is basic, but I didn’t have it before. I was stuck. With enough ritalin and diet coke I was able to keep banging my head on the terminal until a curiously dull exclamation mark would appear above my head and I would make a simple and obvious change in my approach that is assumed knowledge everyone got two minutes after they were taught where the power button was on their machine but I never learned because I don’t play well with others or authority figures.

One interesting twist is that the big bad scary complex command line has been demystified. I kinda got it now. Now when something doesn’t work I have a 50% chance of knowing what question to ask and where to look to find out what I’m not doing correctly. Prior to this point in time I would’ve had a snowballs chance in hell of fixing something on the command line if I broke it. This is what an optimistic person would call progress.

I started as a graphic designer (layout monkey) and it’s been a long road of learning by necessity how to make stuff with html, css, php, mysql, javascript, and now ruby/rails. Still learning. Always have been. Always will be. It’s a bitch.

Posted by jeremy 19/11/2005 at 04h00