If I try to check for values in my database, I sometimes get errors related to the fact that there were no hits from my query.
So I started using .present? to check if the query returned any result, before doing the original check.
Is there a smoother way to avoid nilClass errors than this?
temp = Event.where(basket_id: basket.basket_id).where(event_type: ["operatorJoined", "operatorDisconnected"]).last
if temp.present? && temp.event_type == "operatorJoined"
You could be writing:
if temp && temp.event_type == "operatorJoined"
... or you might like to look at the Sandy Metz presentation "Nothing is Something" to learn more about avoiding this problem by using the Null Object pattern.
I would really hope this code would be something like:
temp = Basket.events.operator_join_or_disc.last
if temp && temp.operator_joined?
Ruby 2.3 introduced a safe call operator (I've seen it called safe navigation operator quite a lot, as well) &. which is similar to the try! method in rails:
class A
attr_accessor :the_other_a
def do_something
puts "doing something on #{object_id}"
end
end
a = A.new
a.do_something # => shows 'doing something on 70306921519040'
a.the_other_a.do_something # => NoMethodError: undefined method `do_something' for nil:NilClass
a.the_other_a&.do_something # => nil
a.the_other_a = A.new
a.the_other_a&.do_something # => shows 'doing something on 70306910376860'
a.the_other_a&.do_something_else # => NoMethodError: undefined method `do_something_else' for #<A:0x007fe334d62738>
a.the_other_a.try(:do_something_else) # => nil
a.the_other_a.try!(:do_something_else) # => NoMethodError: undefined method `do_something_else' for #<A:0x007fe334d62738>
So, in your example something like this should work:
temp = Event.where(basket_id: basket.basket_id).where(event_type: ["operatorJoined", "operatorDisconnected"]).last
if temp&.event_type == "operatorJoined"
However, present? just checks for !blank?, so if the variable (temp in this example) could be false, '', ' ', [], or {} (among any other things that would return true for blank?) then temp.present? && temp.something_else would not be the same as temp&.something_else. Doesn't apply in this situation, since it's the result of the ActiveRecord query, but something to keep in mind.
I have an object with a boolean var.
field :processing, :type => Boolean
The dev before me wrote some code that says this.
:processing => nil
(He is, for some reason, setting it to nil instead of false.)
He then does this if statement
return if self.processing
dosomethingelse....
If I write code that does this
:processing => false
what happens the next time this code runs? Does dosomethingelse run?
return if self.processing
dosomethingelse....
UPDATE ===========
To many questions below so will answer here.
I added this
field :processing, :type => Boolean, :default => false
and it broke the app. When I changed to the above dosomethingelse never gets run?
return if self.processing returns. Any suggestions?
UPDATE 2 =======================================
Here is every reference to processing in my code (redacted). Also I am using MongoDB if that matters.
.where(:processing => nil).gt(:retries => 0).asc(:send_time).all.entries
if self.processing
end
return if self.processing
self.update_attributes(:processing => true)
dosomethingelse....
.where(:sent_time => nil).where(:processing => nil).gt(:retries => 0).asc(:send_time).all.entries
:processing => nil
Ruby uses truthy and falsey.
false and nil are falsey, everything else is truthy.
if true
puts "true is truthy, duh!"
else
puts "true is falsey, wtf!"
end
Output is "true is truthy, duh!"
if nil
puts "nil is truthy"
else
puts "nil is falsey"
end
Output is "nil is falsey"
if 0
puts "0 is truthy"
else
puts "0 is falsey"
end
Output is "0 is truthy"
See this explanation True and False
You could use double negating to "cast" an object to a boolean value:
!!nil # false
!!false # false
!!true # true
In general, only nil and false gives false as result. So, in if statements nil and false are interchangeable.
Yes, dosomethingelse gets run.
In ruby (almost nearly absolutely) everything is an object and every object is either "truthy" or "falsey". In general, everything is "truthy" except for the two constants, nil and false. This means that the code if foo != nil can be written more succinctly as if foo. You are branching based on the "nilness" of a particular value - similar to how you might more explicitly check for foo == null in more traditional languages.
A pattern where this shows up a lot is with ruby Hashes. By default a hash returns nil if a key is missing. So you might have code that works like this:
def foo(opts = {}) # Optional named arguments
# If :bar is not found, than => nil, so the first part of the conditional
# evalutates to false and we return the result of the second expression
bar = opts[:bar] || default_bar
end
There is an important caveat though! false and nil are not the same. Both semantically and in practice. Sometimes you actually want a boolean and then you need to be sure that you are checking explicitly for either that boolean or for nil (depending on what you are testing).
def display(opts = {})
# This will always result in fullscreen = true!
fullscreen = opts[:fullscreen] || true
end
I'm using Ruby 1.8.7 with Rails 3.0.1 and am having a problem whose root cause appears to be the "Array === object" operation. I saw the same behavior before in a class of my own creation, and programmed around it by not using the "===" operator (I assumed that there was some flaw in my knowledge of Ruby, which is still rather limited). But now that it is happening inside ActionPack, I need to do something about it.
This surfaced when the FormHelper "fields_for" was not acting the way it should. The following view code snippet ("<% %>" removed to improve readability):
form_for #coupon do |f|
...
f.fields_for #coupon.participants do |cp|
...
end
end
gave the error "ActionView::Template::Error (undefined method `model_name' for Array:Class):"
inside the form_for helper method. I determined that it was executing the wrong branch of a "case" command, set a breakpoint and started testing. Here are the results:
/Library/Ruby/Gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/form_helper.rb:1152
case record_or_name_or_array
(rdb:1) pp record_or_name_or_array.instance_of? Array
true
(rdb:1) pp Array === record_or_name_or_array
false
(rdb:1) pp Array.object_id
2148267660
(rdb:1) pp record_or_name_or_array.class.object_id
2148267660
This shows pretty definitively that, while "record_or_name_or_array" is definitely an array, "Array === record_or_name_or_array" is returning false.
BTW, in case you're suspecting that "#f.fields_for" is the wrong syntax, I tried it both with and without the "#f." and got the same result. I have also restarted RoR and my machine and the results remain unchanged.
Try this:
#coupon = Coupon.last
Array === #coupon.participants #=> false
Array === #coupon.participants.find(:all) #=> true
Association #coupon.participants is not an array, it is a proxy. The reason why #coupon.participants.class == Array is true is described in activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:25
Added: Another interesting experiment would be #coupon.participants.superclass.
From the console (rails c) try running:
#coupon = Coupon.last
Array == #coupon.participants
If that call returns false, it is most likely that your associations are incorrectly setup (i.e. has_many :participants and belongs_to :coupon).
#coupon.is_a? Array should return true, #coupon === Array would mean #coupon was equal to the singleton instance of Array
This question already has answers here:
How to avoid NoMethodError for nil elements when accessing nested hashes? [duplicate]
(4 answers)
Closed 7 years ago.
In Rails we can do the following in case a value doesn't exist to avoid an error:
#myvar = #comment.try(:body)
What is the equivalent when I'm digging deep into a hash and don't want to get an error?
#myvar = session[:comments][#comment.id]["temp_value"]
# [:comments] may or may not exist here
In the above case, session[:comments]try[#comment.id] doesn't work. What would?
You forgot to put a . before the try:
#myvar = session[:comments].try(:[], #comment.id)
since [] is the name of the method when you do [#comment.id].
The announcement of Ruby 2.3.0-preview1 includes an introduction of Safe navigation operator.
A safe navigation operator, which already exists in C#, Groovy, and
Swift, is introduced to ease nil handling as obj&.foo. Array#dig and
Hash#dig are also added.
This means as of 2.3 below code
account.try(:owner).try(:address)
can be rewritten to
account&.owner&.address
However, one should be careful that & is not a drop in replacement of #try. Take a look at this example:
> params = nil
nil
> params&.country
nil
> params = OpenStruct.new(country: "Australia")
#<OpenStruct country="Australia">
> params&.country
"Australia"
> params&.country&.name
NoMethodError: undefined method `name' for "Australia":String
from (pry):38:in `<main>'
> params.try(:country).try(:name)
nil
It is also including a similar sort of way: Array#dig and Hash#dig. So now this
city = params.fetch(:[], :country).try(:[], :state).try(:[], :city)
can be rewritten to
city = params.dig(:country, :state, :city)
Again, #dig is not replicating #try's behaviour. So be careful with returning values. If params[:country] returns, for example, an Integer, TypeError: Integer does not have #dig method will be raised.
The most beautiful solution is an old answer by Mladen Jablanović, as it lets you to dig in the hash deeper than you could with using direct .try() calls, if you want the code still look nice:
class Hash
def get_deep(*fields)
fields.inject(self) {|acc,e| acc[e] if acc}
end
end
You should be careful with various objects (especially params), because Strings and Arrays also respond to :[], but the returned value may not be what you want, and Array raises exception for Strings or Symbols used as indexes.
That is the reason why in the suggested form of this method (below) the (usually ugly) test for .is_a?(Hash) is used instead of (usually better) .respond_to?(:[]):
class Hash
def get_deep(*fields)
fields.inject(self) {|acc,e| acc[e] if acc.is_a?(Hash)}
end
end
a_hash = {:one => {:two => {:three => "asd"}, :arr => [1,2,3]}}
puts a_hash.get_deep(:one, :two ).inspect # => {:three=>"asd"}
puts a_hash.get_deep(:one, :two, :three ).inspect # => "asd"
puts a_hash.get_deep(:one, :two, :three, :four).inspect # => nil
puts a_hash.get_deep(:one, :arr ).inspect # => [1,2,3]
puts a_hash.get_deep(:one, :arr, :too_deep ).inspect # => nil
The last example would raise an exception: "Symbol as array index (TypeError)" if it was not guarded by this ugly "is_a?(Hash)".
The proper use of try with a hash is #sesion.try(:[], :comments).
#session.try(:[], :comments).try(:[], commend.id).try(:[], 'temp_value')
Update: As of Ruby 2.3 use #dig
Most objects that respond to [] expect an Integer argument, with Hash being an exception that will accept any object (such as strings or symbols).
The following is a slightly more robust version of Arsen7's answer that supports nested Array, Hash, as well as any other objects that expect an Integer passed to [].
It's not fool proof, as someone may have created an object that implements [] and does not accept an Integer argument. However, this solution works great in the common case e.g. pulling nested values from JSON (which has both Hash and Array):
class Hash
def get_deep(*fields)
fields.inject(self) { |acc, e| acc[e] if acc.is_a?(Hash) || (e.is_a?(Integer) && acc.respond_to?(:[])) }
end
end
It can be used the same as Arsen7's solution but also supports arrays e.g.
json = { 'users' => [ { 'name' => { 'first_name' => 'Frank'} }, { 'name' => { 'first_name' => 'Bob' } } ] }
json.get_deep 'users', 1, 'name', 'first_name' # Pulls out 'Bob'
say you want to find params[:user][:email] but it's not sure whether user is there in params or not. Then-
you can try:
params[:user].try(:[], :email)
It will return either nil(if user is not there or email is not there in user) or otherwise the value of email in user.
As of Ruby 2.3 this gets a little easier. Instead of having to nest try statements or define your own method you can now use Hash#dig (documentation).
h = { foo: {bar: {baz: 1}}}
h.dig(:foo, :bar, :baz) #=> 1
h.dig(:foo, :zot) #=> nil
Or in the example above:
session.dig(:comments, #comment.id, "temp_value")
This has the added benefit of being more like try than some of the examples above. If any of the arguments lead to the hash returning nil then it will respond nil.
#myvar = session.fetch(:comments, {}).fetch(#comment.id, {})["temp_value"]
From Ruby 2.0, you can do:
#myvar = session[:comments].to_h[#comment.id].to_h["temp_value"]
From Ruby 2.3, you can do:
#myvar = session.dig(:comments, #comment.id, "temp_value")
Another approach:
#myvar = session[:comments][#comment.id]["temp_value"] rescue nil
This might also be consider a bit dangerous because it can hide too much, personally I like it.
If you want more control, you may consider something like:
def handle # just an example name, use what speaks to you
raise $! unless $!.kind_of? NoMethodError # Do whatever checks or
# reporting you want
end
# then you may use
#myvar = session[:comments][#comment.id]["temp_value"] rescue handle
When you do this:
myhash[:one][:two][:three]
You're just chaining a bunch of calls to a "[]" method, an the error occurs if myhash[:one] returns nil, because nil doesn't have a [] method. So, one simple and rather hacky way is to add a [] method to Niclass, which returns nil: i would set this up in a rails app as follows:
Add the method:
#in lib/ruby_extensions.rb
class NilClass
def [](*args)
nil
end
end
Require the file:
#in config/initializers/app_environment.rb
require 'ruby_extensions'
Now you can call nested hashes without fear: i'm demonstrating in the console here:
>> hash = {:foo => "bar"}
=> {:foo=>"bar"}
>> hash[:foo]
=> "bar"
>> hash[:doo]
=> nil
>> hash[:doo][:too]
=> nil
Andrew's answer didn't work for me when I tried this again recently. Maybe something has changed?
#myvar = session[:comments].try('[]', #comment.id)
The '[]' is in quotes instead of a symbol :[]
Try to use
#myvar = session[:comments][#comment.id]["temp_value"] if session[:comments]
Basically just a check to make sure a url param was set. How I'd do it in PHP:
if(isset($_POST['foo']) && isset($_POST['bar'])){}
Is this the rough/best equivalent to isset() in RoR?
if(!params['foo'].nil? && !params['bar'].nil?) end
The closer match is probably #present?
# returns true if not nil and not blank
params['foo'].present?
There are also a few other methods
# returns true if nil
params['foo'].nil?
# returns true if nil or empty
params['foo'].blank?
You can also use defined?
See example from: http://www.tutorialspoint.com/ruby/ruby_operators.htm
foo = 42
defined? foo # => "local-variable"
defined? $_ # => "global-variable"
defined? bar # => nil (undefined)
Many more examples at the linked page.
Yes. .nil? is the equivalent of isset() in that case when checking the existence of a key in a Hash.
You should use Hash's key? method, which returns true if the given key is present in the receiver:
if(params.key?('foo') && params.key?('bar')) end
I think the most important thing, when you migrating from PHP to ROR, is the understanding of the fact that in Ruby everything is true except false and nil
So, your code:
if(!params['foo'].nil? && !params['bar'].nil?){}
is equivalent for:
if(params['foo'] && params['bar']) end
and this is full equivalent for your PHP code.