Sorry folks if this was raised already, but google is not very
useful when asked for "ruby with" or "ruby map single object" ...


Is there something like "with" already available? Let me give the
short implementation first so the gurus know right away what I am after:

module Kernel
  def with
    yield self
  end
end

What does it do? It can glue an object and a block together. Just
append ".with {|x| block}" to your object. What's that good for?

When I have an array (or range or similar) and I want to do
something with each member, I can already do that by simply adding
".map {|elem| do_something_with(elem)}". The result can be used
transparently within another calculation. #each, #map and #inject
are good candidates for something like that. So far, so good.

Take the following situation:

> result= a_long_calculation_start ...
>  ... a_very_long_object_calculation ...
>  ... calculation_end

What can I do now if I have to apply a function on
"a_very_long_object_calculation" in the example above? Since
a_very_long_object_calculation is not an array, I cannot use
#each/#map/#inject. So let's put the function call around the
calculation:

Solution 1:

> result= a_long_calculation_start ...
>  ... function(a_very_long_object_calculation) ...
>  ... calculation_end

But what if "function" is an in-place calculation containing of if's
and loops? Then I have to move all that away from the long object
calculation:

Solution 2:

> intermediate= a_very_long_object_calculation
> intermediate_result= function(intermediate)
> result= a_long_calculation_start ...
>  ... intermediate_result ...
>  ... calculation_end

Obviously, while reaching the goal, the function is now ugly
disrupted. Even Solution 1 suffers from the fact that I have to at
least put some text into the expression before
a_very_long_object_calculation appears. What would be nice if I
could implement the in-place calculation inside of a block like with
#each, #map or #inject:

> result= a_long_calculation_start ...
>  ... a_very_long_object_calculation.with{|val| function(val)} ...
>  ... calculation_end

With "with", the following proportions are solved:

1. I can put arbitrary functions into the block following #with.

2. I don't have to put additional characters before
a_very_long_object_calculation -- succeeding calculations are behind
the preceding object calculation.

Am I the first to ask for something like this or is this already
implemented in a way that I couldn't find it?

BTW: "with" could be renamed to something short and similar -- f.e.
.smap (single map), so there could be a .seach (single each) as well
returning "self" after the yield. .sinject is not very useful.

module Kernel
  def smap
    yield self
  end
  def seach
    yield self
    self
  end
end

Thanks,
- Matthias