Just to clarify the context here is that you can’t have type-safe access to params when you have multiple methods in a single class. If you do something like
params[:param_name] then you don’t need
not_nil! but that is because there is never a guarantee that the param will exist.
So here is kind of what I mean by needing
puts id # This will raise at Runtime because `id` is not set for this route
puts id # This will work
So this doesn’t use
not_nil! but illustrates that the param can be
nil and can lead to Runtime errors.
You could use
not_nil! but you still have to manually ensure you are using it correctly. There is no compile-time guarantee
puts id.not_nil! # This will raise at Runtime because `id` is not set for this route
puts id.not_nil! # This will work
Whereas in Lucky each action has a route defined and thereforce can guarantee that a param exists at compile-time:
class Users::Show < BrowserAction
# Lucky generates an 'id' method that returns the id
route "/users/:id" do
puts id # Works
This also helps with path generation because the param is required at compile-time:
Users::Show.path(id: 123). There are other ways to do that, but the class-based approach made it pretty simple.
Does that make sense?
EDIT: you actually can as @Blacksmoke16 showed below! I’d like to correct my previous statement to can’t do type safe to access params using instance methods/instance vars for the param (I think ).