Is It Idiomatic Ruby to Add an Assert( ) Method to Ruby's Kernel Class

Is it idiomatic Ruby to add an assert( ) method to Ruby's Kernel class?

No it's not a best practice. The best analogy to assert() in Ruby is just raising

 raise "This is wrong" unless expr

and you can implement your own exceptions if you want to provide for more specific exception handling

Asserts in Rails from models or controllers?

There are many assert functions if you are writing tests. But for assertiona in the main code, there aren't any and you can roll your own easily.

Add something like this to environment.rb:

class AssertFailure < Exception
end

def assert(message = 'assertion failed')
unless block_given? and yield
raise message
end
end

and make it a no-op in your environments/production.rb so there is minimal overhead

def assert(message = 'assertion failed')
end

Then, in your code, you can assert to your heart's content:

assert { value == expected_value }
assert('value was not what was expected') { value == expected_value }

If value does not equal expected_value and you aren't running in production, an exception will be raised.

Ruby: assert error message produced by a subclass of Set

test/unit appears to not rely on an object's to_s or inspect method. Looking at the source, it may be accessing the object's class inspect method directly, but my attempts to redefine Sets inspect instance method didn't work either. Check out the source of test/unit. ;-)

Creating multiple instances of a class. Most idiomatic?

TL;DR

Modeling business domain objects isn't trivial. For robust real-world inventory management, you'll probably want a real database rather than "plain old Ruby objects" (POROs) to manage CRUD operations and data normalization, but you can certainly model your problem domain using some fairly basic POROs like Array and Struct.

Domain Modeling Example and Sample Usage

I may be misunderstanding your question, but it seems like the problem is more about how to properly model the domain than anything else. You really need a fully normalized database with a schema that describes the items in your inventory (and likely each item's location data such as aisles and shelves) rather than complex Ruby objects.

That said, if you're just trying to find a way to represent your current data set in plain Ruby, you might consider the following approach:

require 'singleton'

class Inventory
include Singleton
attr_accessor :items

def initialize
@items = []
end
end

class Item < Struct.new(:name, :price, :quantity, :promo_group)
end

inventory = Inventory.instance

inventory.items.append(
Item.new('apple', 0.99, 5, 'A'),
Item.new('orange', 1.50, 7, 'B'),
)

Basically, the idea is to create a singleton Inventory object that contains an array of Item objects. Each Item object is a Struct that contains whatever data you may need. You manipulate the inventory through the Inventory#items getter and setter methods, or reference the attributes of a single Item in the inventory through the Enumerable-like behavior of its Struct. For example:

inventory.items
#=> [#<struct Item name="apple", price=0.99, quantity=5, promo_group="A">, #<struct Item name="orange", price=1.5, quantity=7, promo_group="B">]

inventory.items.map &:name
#=> ["apple", "orange"]

inventory.items.select { _1.promo_group == ?A }
#=> [#<struct Item name="apple", price=0.99, quantity=5, promo_group="A">]

inventory.items.filter { _1.name == 'apple' }.count
#=> 1

inventory.items.filter { _1.name == 'apple' }.map &:to_h
#=> [{:name=>"apple", :price=>0.99, :quantity=>5, :promo_group=>"A"}]

This solves for the question you asked, but you'll have to build your own methods for basic operations like searching, adding, deleting, or updating specific inventory items. A real database with object-relational mapping (e.g. an ORM interface like ActiveRecord, Sequel, or similar on top of a SQL or NoSQL database) is really a better solution for a real-world application.

When does Ruby know that a method exists?

It doesn't know, and it doesn't care - until execution. When a method call statement is executed, the interpreter looks to see if the class (object, not code!) has the named function. If it does not, it looks up the ancestor tree. If it does not find any, it calls the method_missing method. If that is not defined, you get your error.

If your function call does not get executed, you will not get any errors.

Does Specflow's CompareToSet() need an Assert?

Having an exception is enough.

Don't forget, SpecFlow doesn't actually run your tests. It delegates that to NUnit/xUnit/MsTest. So if your code would fail the test in one of those it should also fail in SpecFlow.

BTW, If you write your test first before you write your functional code then of course your test will fail, and that process checks that your fail will work, so you would see for yourself if this is sufficient. ;-)

What is a Pointer?

Pointers are not as hard as they sound. As others have said already, they are variables that hold the address of some other variable. Suppose I wanted to give you directions to my house. I wouldn't give you a picture of my house, or a scale model of my house; I'd just give you the address. You could infer whatever you needed from that.

In the same way, a lot of languages make the distinction between passing by value and passing by reference. Essentially it means will i pass an entire object around every time I need to refer to it? Or, will I just give out it's address so that others can infer what they need?

Most modern languages hide this complexity by figuring out when pointers are useful and optimizing that for you. However, if you know what you're doing, manual pointer management can still be useful in some situations.

Class not registered exception: BitmapImage

You need to run an actual packaged WinUI app or deploy the Windows App SDK runtime along with a non-MSIX-packaged app to be able to use the WinUI types.

I am afraid it's not as simple as simply referencing the WinUI code from a standalone testrunner app.

Please refer to this blog post for an example of how to set up an actual app that runs the tests. Or avoid creating UI specific stuff in your unit tests.



Related Topics



Leave a reply



Submit