Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7997129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:53:48+00:00 2026-06-04T14:53:48+00:00

Say I have function with_foo that takes a block, and wrap it around a

  • 0

Say I have function with_foo that takes a block, and wrap it around a piece of code, like

with_foo do
  puts "hello!"
end

Now I would like to make the wrapping conditional, like

if do_with_foo?
  with_foo do
    puts "hello!"
  end
else
  puts "hello!" # without foo
end

Is there any way to write this shorter/more elegantly, meaning without having to repeat the code puts "hello!"?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:53:49+00:00Added an answer on June 4, 2026 at 2:53 pm

    if you are willing to specify argument with a block, it is possible.

    given with foo above, you can write such snippet:

    whatever = proc {puts "hello"}
    #build a proc object with a block
    if do_with_foo?
      with_foo &whatever
    #pass it to with_foo
    else
      whatever.call
    #normally call it
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a function that takes an arbitrary number of arguments (the last
Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
Say I have a function func(i) that creates an object for an integer i,
Say I have a function foo that I want to call n times. In
In C++ it's OK to have a funcction that takes a function local type:
Lets say I have a class like this in Java: public class Function {
Say I have some code like so: private static Thing t = null; private
Say I have a Javascript class defined and instantiated like this: Demo = function()
I have a function (say foo() )that will be called from time to time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.