[#71439] [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API — matz@...
Issue #11339 has been updated by Yukihiro Matsumoto.
7 messages
2015/11/11
[#71473] Re: [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API
— Eric Wong <normalperson@...>
2015/11/13
Entire series for sockets
[#71450] Ruby 2.3.0-preview1 Released — "NARUSE, Yui" <naruse@...>
Hi,
5 messages
2015/11/11
[#71617] [Ruby trunk - Feature #11664] [PATCH] introduce rb_autoload_value to replace rb_autoload — nobu@...
Issue #11664 has been updated by Nobuyoshi Nakada.
3 messages
2015/11/20
[#71721] [Ruby trunk - Feature #11741] Migrate Ruby to Git from Subversion — me@...
Issue #11741 has been updated by Jon Moss.
4 messages
2015/11/28
[ruby-core:71595] [Ruby trunk - Feature #11717] Object#trap -- pass object to block and return result
From:
matthew@...
Date:
2015-11-19 23:08:38 UTC
List:
ruby-core #71595
Issue #11717 has been updated by Matthew Kerwin.
Some related issues and historical readings:
* #10095 `Object#as`
* from #6373 `Object#self`
* #6721 `Object#yield_seld`
* #6684 `Object#do`
* #7388 `Object#embed`
Clearly this is something the Ruby community wants. Just as clearly, it's something nobody can name.
----------------------------------------
Feature #11717: Object#trap -- pass object to block and return result
https://bugs.ruby-lang.org/issues/11717#change-54977
* Author: Victor Shepelev
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
`Object#trap` can be thought as useful counterpart for `Object#tap`: like tap, it passes object to the block; **unlike** tap, it returns results of the block, not object itself.
**Rationale**
`#trap` could allow to gracefully chain processing of objects, which isn't `Enumerable`, therefore enforcing "functional" style in Ruby (which considered good).
**Use case**
~~~
# Assume we grab some resource from web:
SomeWebClient.get('http://some/url', param1: 'value1', param2: 'value2').body
# And now, the body of response is JSON, and we want it parsed. How to express it?
# Option 1: wrap:
JSON.parse(SomeWebClient.get('http://some/url', param1: 'value1', param2: 'value2').body)
# Downside: messy parenthesis, and need to jump back and forth to understand the meaning
# Option 2: intermediate variable:
s = SomeWebClient.get('http://some/url', param1: 'value1', param2: 'value2').body
JSON.parse(s)
# Downside: intermediate variable is not elegant
# Option 3: monkey-patch (or refine)
SomeWebClient.get('http://some/url', param1: 'value1', param2: 'value2').body.from_json
# Downside: monkey-patching is a last resort; also, your classes should be already patched when you stuck with this case
# Option 4 (proposed): trap
SomeWebClient.get('http://some/url', param1: 'value1', param2: 'value2').body.
trap{|s| JSON.parse(s)} # => parsed JSON
~~~
And when you are thinking with code, experimenting with code (especially in irb, but in editor too), only last option is "natural" river of thoughts: do this, then do that (extract data from web, then parse it).
**Naming**
* it is similar enough to `tap`;
* it is specific enough to not be used widely in some popular library (or isn't it?);
* mnemonic is "do something and trap (catch) the value".
WDYT?
--
https://bugs.ruby-lang.org/