Ruby's "Foo = True If !Defined? Foo" Won't Work as Expected

Ruby's foo = true if !defined? foo won't work as expected

Ruby defines a local variable just before executing a line containing an assignment, so defined?(foo) will always be true for the one-liner.

Another example showing that local variables are defined before any part of the line are executed:

defined? foo # => false
foo = foo # => foo is now nil

Ruby's foo = true if !defined? foo || foo.nil? won't work

Be careful when skipping parenthesis. You meant:

foo = true if !defined?(foo) || foo.nil?

As per your other question, the defined?(foo) will always be true, so really you want to write:

foo = true if foo.nil?

Why doesn't TestClass#foo work as expected? (Details and example below)

This is just the output being buffered. You can force output to be written immediatley and not buffered with IO#sync=.

For example:

STDOUT.reopen(@log_path, 'a')
STDOUT.sync = true

unless defined? is not working in my code

defined? method will return:

nil => expression not recognizable

The problem in the above snippet is the scope of the local variable. Its end on the line where you using it. To learn more about local variable, please check this: local_variable

pry(main)> p "local_var is not initialized" unless defined? local_var
=> "loca_var is not initialized"

but if you do this:

pry(main)> local_var = "initialized" unless defined? local_var
=> nil

local_var is still nil because its scoped end after that line, so whatever assigned were wasted.

Solution: I will suggest if you want this behaviour then use this one:

local_var ||= "initialized"

Why doesn't this inline if test work?

It fails because as soon as a bareword is seen on the left-hand side of an assignment, it's initialized to nil. Thus when you do (bar1 = foo) if (!defined?(bar1) && foo), bar1 will be nil in the defined? check. Here's a simplified example:

>> defined? foo
=> nil
>> foo = 1 if false
=> nil
>> foo
=> nil
>> defined? foo
=> "local-variable"

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

In Ruby, how do I check if method foo=() is defined?

The problem is that the foo= method is designed to be used in assignments. You can use defined? in the following way to see what's going on:

defined?(self.foo=())
#=> nil
defined?(self.foo = "bar")
#=> nil

def foo=(bar)
end

defined?(self.foo=())
#=> "assignment"
defined?(self.foo = "bar")
#=> "assignment"

Compare that to:

def foo
end

defined?(foo)
#=> "method"

To test if the foo= method is defined, you should use respond_to? instead:

respond_to?(:foo=)
#=> false

def foo=(bar)
end

respond_to?(:foo=)
#=> true

expected valid? to return true, got false

I fixed it, I got a help from this link - Click Here

I think it is my VALID_EMAIL_REGEX was wrong

VALID_EMAIL_REGEX = /\A[w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Actual is :

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Next time I will place code on github and place here a link, so that is

Re-use failure message in rspec custom matcher

There is no direct method available to yield original error, I would suggest you to write your own logic to generate similar message.

If you still want to use the existing method, there is a private method which you can call and it will return the default error message. You may need to set some instance variables expected_value, actual_value etc.

RSpec::Matchers::BuiltIn::ContainExactly.new(expected_value).failure_message

reference code



Related Topics



Leave a reply



Submit