18 Matching Annotations
- Feb 2024
-
github.com github.com
-
Its a bit tricky because of the ambiguity of how the args get presented. You can see through the little demo the args are presented the same way whether its a straight kwargs or a hash, but the assignment of the args to parameters is different. def foo(*args) puts args.inspect end def bar(x=1, a:2) puts "x:#{x} a:#{a}" end foo(:a => 1) # [{:a=>1}] foo({:a => 1}) # [{:a=>1}] bar(:a => 1). # x:1 a:1 bar({:a => 1}). # x:{:a => 1} a:2
-
-
github.com github.com
-
“…” would be cool, but (unless I’ve missed something in Ruby 3.2+), it can only delegate all args, and foo(1, …) is impossible.
-
-
github.com github.com
-
my_func(1, 2, 'foo' => 1, :kw => true) # ArgumentError: unknown keyword: "foo" even though Hash.ruby2_keywords_hash?(args.last) returns true.
-
- Oct 2022
-
stackoverflow.com stackoverflow.com
-
opts = method(__method__).parameters.map(&:last).map { |p| [p, eval(p.to_s)] }.to_h SomeOtherObject.some_other_method(opts)
-
that's right, we don't want to do params = { ... } because then we're hardcoding the implementation and it becomes very coupled. The benefit of doing it like in my examples is that you can change the method signature and still automatically capture all keyword parameters.
-
- Sep 2022
-
-
Ruby's keyword args work a little differently than similar implementations (selectors in Objective C, for instance). The value on the right-hand side of the colon is the default, not the local name.
-
- Aug 2021
-
-
Provides empty Module#ruby2_keywords method, for the forward source-level compatibility against ruby2.7 and ruby3.
-
-
www.ruby-lang.org www.ruby-lang.org
-
3. The no-keyword-arguments syntax (**nil) is introduced You can use **nil in a method definition to explicitly mark the method accepts no keyword arguments. Calling such methods with keyword arguments will result in an ArgumentError. (This is actually a new feature, not an incompatibility)
-
This is useful to make it explicit that the method does not accept keyword arguments. Otherwise, the keywords are absorbed in the rest argument in the above example.
-
If you extend a method to accept keyword arguments, the method may have incompatibility as follows: # If a method accepts rest argument and no `**nil` def foo(*args) p args end # Passing keywords are converted to a Hash object (even in Ruby 3.0) foo(k: 1) #=> [{:k=>1}] # If the method is extended to accept a keyword def foo(*args, mode: false) p args end # The existing call may break foo(k: 1) #=> ArgumentError: unknown keyword k
-
If your code doesn’t have to run on Ruby 2.6 or older, you may try the new style in Ruby 2.7. In almost all cases, it works. Note that, however, there are unfortunate corner cases as follows:
-
Ruby 2.6 or before themselves have tons of corner cases in keyword arguments.
-
-
ruby2_keywords allows you to run the old style even in Ruby 2.7 and 3.0.
-
- Jul 2021
- Feb 2021
-
trailblazer.to trailblazer.to
-
Keyword arguments allow to define particular parameters as required. Should the parameter be missing, they also provide a way to set a default value. This is all done with pure Ruby.
-
-
trailblazer.to trailblazer.to
-
You may use keyword arguments in your filters for type safety and better readable code.
-
- Feb 2020
-
blog.saeloun.com blog.saeloun.com
-
The **nil argument was added in Ruby 2.7 to explicity mark that the method accepts no keyword arguments.
-