0

There are several situations where I'd like to apply a block to a certain value and use the value inside this block, to use the enumerator coding style to every element.

If such method would be called decompose, it would look like:

result = [3, 4, 7, 8].decompose{ |array| array[2] + array[3] } # result = 15

# OR

result = {:key1 => 'value', :key2 => true}.decompose{ |hash| hash[:key1] if hash[:key2] } # result = 'value'

# OR

[min, max] = [3, 4, 7, 8].decompose{ |array| [array.min, array.max] } # [min, max] = [3, 8]

#  OR

result = 100.decompose{ |int| (int - 1) * (int + 1) / (int * int) } # result = 1

# OR

result = 'Paris'.decompose{ |str| str.replace('a', '') + str[0] } # result = 'PrisP'
3
  • Maybe you should include the output, too. Commented Oct 15, 2014 at 9:24
  • This has been asked before but I can't find the question ... Commented Oct 15, 2014 at 9:26
  • Been adding the outputs. Thought it was straightforward. I'm sure something exists (or could easily be monkeypatched to every class) but I can't find it myself either... Commented Oct 15, 2014 at 9:29

2 Answers 2

1

The method simply yields self to the block, returning the block's result. I don't think it exists, but you can implement it yourself:

class Object
  def decompose
    yield self
  end
end

[3, 4, 7, 8].decompose{ |array| array[2] + array[3] }
#=> 15

{:key1 => 'value', :key2 => true}.decompose{ |hash| hash[:key1] if hash[:key2] }
#=> "value"

[3, 4, 7, 8].decompose{ |array| [array.min, array.max] }
#=> [3, 8]
Sign up to request clarification or add additional context in comments.

6 Comments

The tap Object method almost does that...
And this post refers to it too: stackoverflow.com/questions/26378890/…
Is it me or you've just posted your own question as a reference in the comment of the answer of your question referring to your question again which is mentioned by you in your comment as a reference to your own question and so on..?
Ok I realize now the mistake. :) I wanted to say that the question is related to this one: stackoverflow.com/questions/7878687/…
@AugustinRiedinger not really, tap returns the object, not the result of the block.
|
0

It actually exists (I could not believe it didn't).

It is called BasicObject#instance_eval. Here's the doc: http://apidock.com/ruby/BasicObject/instance_eval

Available since Ruby 1.9 as this post explains: What's the difference between Object and BasicObject in Ruby?

5 Comments

instance_eval does much more, it evaluates the block within the context of the receiver.
What does it mean? At least it does exactly what I was looking for!
Take a look at the example in the docs. instance_eval lets you inspect (and modify) the receiver's internal state.
Which the yield self doesn't?
No it doesn't. It just passes the receiver to the block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.