Is there a ruby method that just returns the value of a block?

I know about Object#tap, which takes a value and returns that value. But is there a method that takes a block and returns the value evaluated by the block?

To improve my code in this answer (which is more complicated than the snippet below), I'd like to change

deck.index("A").tap {|index| 
  STDERR.puts "Result of indexing for #{"A".inspect} is #{index.inspect}"
}

, which has "A" repeated, into

def my_method(*args)
  yield *args
end

deck = ['A', 'B', 'C']
my_method("A") {|value| deck.index(value).tap {|index|
  STDERR.puts "Result of indexing for #{value.inspect} is #{index.inspect}"
} }
# Result of indexing for "A" is 0
# => 0

Answers


What you're looking for is essentially the equivalent of let in Lisp or OCaml — something that allows you to temporarily bind a value to an identifier without introducing a new variable into the larger scope. There isn't anything that lets you do such a thing with that syntax in Ruby. The equivalent Ruby would be:

lambda {|value| deck.index(value).tap {|index|
  STDERR.puts "Result of indexing for #{value.inspect} is #{index.inspect}"
} }.call 'A'

You could of course just write a method like:

def let(*values) 
  yield *values 
end

Need Your Help

Titanium Developer

ios-simulator android-emulator titanium

I am trying to use the titanium developer v.1.2.1 but i have some difficulty the simulators are not lunching!

jQuery ajax loop containing onclick event only changes last occurrance

ajax arrays jquery for-loop

My page displays posts stored in my database and pairs a like/dislike button to each post.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.