nil

Elegantly escaping errors

I have statements such as @user = User.find(current_user.id) throughout my app. Sometimes a user might enter with a nil variable (such as a new user for whom current_user is nil). I'm sure the dumb way to do this would be to scatter if statements everywhere like... if current_user.exists? @user = User.find(current_user.id) else re...

Programmatically determine if a Cocoa object loaded from nib/xib is available

The setting is the following: I have a cocoa object in a nib file that is loaded when the NSWindow and view is loaded The window can be closed I also access the object programmatically Now what happens in some situations is that I get a crash, when I try to send a message to the object, but it has been deallocated before (because the...

difference between nil and released object

Hi guys, I'm new to Objective-C and and I can't understand this. I know that I can send a message to nil (it's so hyped about feature of Objective-C), but I can't send a message to released object, getting an exception in this case, what the difference between them? ...

Objective-C: Substituting empty string for NSNulls in a constructor

I have a NSDictionary containing attributes I got from deserializing a JSON structure. I would like to initialize an object with these attributes, but replacing all the NSNull values with an empty string (all the object properties are strings). The object is being managed with Core Data. I'm new to the language, so I'm not sure what's t...

How do I avoid nil class in ruby on rails?

I get the following error, and thought by using the .nil? method I could avoid getting an error by identifying the exception. But I don't. Line 40 shows I am getting an error...it seems to think that contact.latest_event is nil. But shouldn't .nil? help me avoid getting the error? Thanks...! ActionView::TemplateError (undefined meth...

Problem with NSMutableArray and custom NSObject

Hi, I'm really desperate on this one. I'm trying to make a Framework which you can search and play YouTube videos with. But while testing it, I'm running in to a big problem. In the search operation I'm adding YTVideos (a subclass of NSObject) to a NSMutableArray. When I loop thru it in the main(), I'm getting nil-objects: Method - (N...

Rails variable loads first time and then is nil!

I am receiving the following error: ActionView::TemplateError (You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.include?) on line #24 of app/views/index/index.html.erb: 21: <% @achievements.each do |achievement| %> 22: <%= achievement.name %> 23: ...

Avoiding nil in Rails views

I'm sure this has been asked already, but I can't find the answer. I have a Project model, which has a belongs_to relationship with my Client model. A client has a name, but a project doesn't necessarily have a client. In my view, I've got code like this: <%=h project.client && project.client.name %> because if the project doesn't h...

Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)

I was going through the exercises at http://rubykoans.com/ and I was struck by the following Ruby quirk that I found really unexplainable: array = [:peanut, :butter, :and, :jelly] array[0] => :peanut #OK! array[0,1] => [:peanut] #OK! array[0,2] => [:peanut, :butter] #OK! array[0,0] => [] #OK! array[2] => :and #OK! array[2,2] ...

What happend here? (nil in Ruby)

p parent.class #=> NilClass # ok. p !!parent # => false # as expected. p parent.object_id # => 17006820 # should be 4 p parent && parent.foo # => NoMethodError foo # should be nil-guarded Where does this object come from? ...

rails nils in a form

This is probably a simple question, but I noticed a few errors deriving from having empty values instead of nils... If a user leaves a field blank in a standard rails app is there a way to leave the field in the database set to NULL instead of entering an empty value? Basically so a check like @model.info.nil? return true instead of ha...

Delphi objects, NIL objects and Interfaces

I am looking for hints on how to debugging a crash in an application that uses the MS XML wrappers in the Delphi VCL. I suspect memory corruption, or some kind of obscure evil thing happening between objects and interfaces, such as reference counting bugs, or heap corruption. The question is, in effect: how do I debug such a crash? T...

Import AddressBook Contact Info, how to handle empty fields?

I am importing contacts info into some textfields, but the app crashes if there are no entries for certain fields, like phone, email, etc. Here are my textfields: First Name Middle Name Last Name Main Phone Mobile Phone Email Address Website Suppose the selected contact doesnt have a second Phone number...

undefined method `to_sym' for nil:NilClass during a db:auto:migrate

I'm trying to run a db:auto:migrate in my system and it keep throwing the undefined method `to_sym' for nil:NilClass whenever it gets to a column that uses a type of date. If I change the field to a datetime and run the migrate, it will change the field to a date with no errors. What is causing this problem. I have to keep changing th...

How to check if an object is nil

I made a Class that has several NSStrings as properties. If I have an object of this class, then how can I know if the object is nil (i.e. all the NSString properties are nil). My class looks like this // MyClass.h #import <Foundation/Foundation.h> @interface MyClass : NSObject <NSCoding> { NSString *string1; NSString *string2; }...

Avoiding, in general, "undefined method 'some_method' for nil:NilClass" in Ruby

Ruby's duck-typing is great, but this is the one way that it bites me in the ass. I'll have some long running text-processing script or something running, and after several hours, some unexpected set of circumstances ends up causing the script to exit with at NoMethodError due to a variable becoming nil. Now, once it happens, it's usua...

How do I use the hash :condition on a .find for a nil? method in Rails (has_one relationship)

I want to find all Contacts where the value of their :has_one relationship to :contact_status does not have a value (does that mean nil?) I created an alias_attribute :status, :status_contact Can I do something along the lines of: contacts = Contact.find(:all, :conditions => {:contact_status => nil }, :include => {:status_contact} )...

if a value from controller is nil, how do I display 0?

I am creating a report which looks for the number of emails sent during a period of time: I display it in the View as follows: <td><%= @emails_sent.size %></td> And it is generated in the Controller as follows: @sent_emails = ContactEmail.all(:conditions => ['date_sent >= ? and date_sent <= ?', @monday, @friday]) But sometimes ...

Binding returns default value (set with registerDefaults:) instead of zero

Here's my setup: myTextField is bound to a key in the Shared User Defaults Controller. The user can only enter numbers in the text field. Each time my application loads, I load default preferences (from the app's Resources folder) using [[NSUserDefaults standardUserDefaults] registerDefaults: ... ]. myMenuItem's title is bound to the s...

nil values magic

I have faced the plain simple situation, that i can't figure out. I have to deside, whether to show the "more" link, depending of the articles count The code is plain simple @no_more = offset + length >= Article.count @no_more variable equals to nil sometimes. I see in debugger, that offset=0, length=10 and Article.count=12 But the...