14 Matching Annotations
- Nov 2022
-
github.com github.com
-
by using symbols as keys, you will be able to use the implicit conversion of a Mash via the #to_hash method to destructure (or splat) the contents of a Mash out to a block
This doesn't actually seem to be an example of destructure/splat. (When it said "destructure the contents ... out to a block", I was surprised and confused, because splatting is when you splat it into an argument or another hash — never a block.)
An example of destructure/splat would be more like
method_that_takes_kwargs(**symbol_mash)
Tags
Annotators
URL
-
- 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
-
medium.com medium.com
-
What if I told you there was a way to do this in Ruby?:destructure def adds(a: 1, b: 2) a + bendadds(a: 1, b: 2)# => 3adds(OpenStruct.new(a: 1, b: 2))# => 3Foo = Struct.new(:a, :b)adds(Foo.new(1,2))# => 3
-
- Jun 2021
-
disqus.com disqus.com
-
The answer is no, we use a pattern where we do this, and have a `static` method for manufacturing the constructor.e.g.static from({prop1, prop2}) => new this(public prop1, public prop2)
-
- Feb 2021
-
stackoverflow.com stackoverflow.com
-
The problem is that you what you want is actually not de-structuring at all. You’re trying to go from 'arg1', { hash2: 'bar', hash3: 'baz' }, { hash1: 'foo' } (remember that 'arg1', foo: 'bar' is just shorthand for 'arg1', { foo: 'bar' }) to 'arg1', { hash1: 'foo', hash2: 'bar', hash3: 'baz' } which is, by definition, merging (note how the surrounding structure—the hash—is still there). Whereas de-structuring goes from 'arg1', [1, 2, 3] to 'arg1', 1, 2, 3
-
-
-
{a: 1, b: 2, c: 3, d: 4} => {a:, b:, **rest} # a == 1, b == 2, rest == {:c=>3, :d=>4}
equivalent in javascript:
{a, b, ...rest} = {a: 1, b: 2, c: 3, d: 4}
Not a bad replacement for that! I still find javascript's syntax a little more easily readable and natural, but given that we can't use the same syntax (probably because it would be incompatible with existing syntax rules that we can't break for compatibility reasons, unfortunately), this is a pretty good compromise/solution that they've come up with.
-
-
we’re going to look how improved pattern matching and rightward assignment make it possible to “destructure” hashes and arrays in Ruby 3—much like how you’d accomplish it in, say, JavaScript
Tags
- ruby: hash object destructuring
- good example
- feature parity
- constant evolution/improvement of software/practices/solutions
- JavaScript
- backwards compatible
- equivalent/analogous/alternative ways to do something between 2 libraries/languages/etc.
- ruby
- improvement
- object destructuring
Annotators
URL
-
- Oct 2020
-
-
A reasonably clean alternative would be to map a function over the array and use destructuring in the each loop: {#each [1, 2, 3, 4].map(n => ({ n, sqr_n: n * n })) as { n, sqr_n }} {sqr_n} {sqr_n / 2}<br> {/each}
-
-
svelte.dev svelte.dev
-
If you prefer, you can use destructuring — each cats as { id, name } — and replace cat.id and cat.name with id and name.
Tags
Annotators
URL
-
- Sep 2020
-
svelte.dev svelte.dev
-
You can freely use destructuring and rest patterns in each blocks.
-
- Aug 2019
-
blog.logrocket.com blog.logrocket.com
-
({ theme = 'secondary', label = 'Button Text', ...restProps })
-
-
stackoverflow.com stackoverflow.com
-
props parameters and assign default values directly:
-
- Jul 2018
-
www.educative.io www.educative.io
-
.
That's the key point
-