nil result despite both #hash and #eql? evaluating to true, when looking up a key in a hash?
In this case, your hash becomes outdated. Use rehash
to solve your problem.
my_hash.rehash
Checking if a variable is not nil and not zero in ruby
unless discount.nil? || discount == 0
# ...
end
Why does Ruby include? evaluate to nil?
Minor nitpick: Array#include?
always returns true
or false
, never nil
.
To answer your question: x.include?(y)
tests whether y
is an element of x
, not whether it's a subarray.
[1,1,1,5,1].include?([1,1,1])
returns false
because [1,1,1]
is not an element of the array [1,1,1,5,1]
. [[1,1,1],[5,1]].include?([1,1,1]))
would return true
.
There is no method in ruby that checks whether an array is a subarray of a another array, but you can easily write it as arr1.each_cons(arr2.size).include?(arr2)
(needs require 'enumerator'
in 1.8.6). This is O(arr1.size*arr2.size)
though.
If you want it in O(arr1.size + arr2.size)
, you can implement the Knuth-Morris-Pratt algorithm (which is meant to find substrings, but works equally well for finding subarrays as they are essentially the same thing).
Why is `a = a` `nil` in Ruby?
Ruby interpreter initializes a local variable with nil
when it sees an assignment to it. It initializes the local variable before it executes the assignment expression or even when the assignment is not reachable (as in the example below). This means your code initializes a
with nil
and then the expression a = nil
will evaluate to the right hand value.
a = 1 if false
a.nil? # => true
The first assignment expression is not executed, but a
is initialized with nil
.
You can find this behaviour documented in the Ruby assignment documentation.
Undeclared instance variables default to nil?
Per the Ruby doc:
Instance variables of ruby do not need declaration. This implies a flexible structure of objects. In fact, each instance variable is dynamically appended to an object when it is first referenced.
https://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/instancevars.html
You can also get a list of all previously defined instance variables:
ClassName.instance_variables
or
a = ClassName.new
a.instance_variables #specific to instance
Related Topics
How to Run Rake Tasks Within a Ruby Script
In Ruby, Is There an Array Method That Combines 'Select' and 'Map'
Read and Write Yaml Files Without Destroying Anchors and Aliases
Getaddrinfo: Nodename Nor Servname Provided, or Not Known
Rails Each Loop Insert Tag Every 6 Items
Checking If a Variable Is Defined in Sass
How to Convert Datetime.Now to Utc in Ruby
What's the Precedence of Ruby'S Method Call
Purpose of "Consider_All_Requests_Local" in Config/Environments/Development.Rb
How to Use "Gets" and "Gets.Chomp" in Ruby
Running Another Ruby Script from a Ruby Script
Understanding Ruby and Os I/O Buffering
Getting a "Bad Interpreter" Error When Using Brew
Ruby Gsub Doesn't Escape Single-Quotes
Storing Objects in a Session in Rails
Ruby Method Lookup Path For an Object
Rails: Specified 'Mysql2' for Database Adapter But the Gem Is Not Loaded