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 921905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:56:32+00:00 2026-05-15T18:56:32+00:00

Possible Duplicate: 'pass parameter by reference' in Ruby? in this example: def test verb

  • 0

Possible Duplicate:
'pass parameter by reference' in Ruby?

in this example:

def test
  verb = 'nothing'
  yield verb
  puts verb
end

test {|verb| verb = 'something'}

it will print “nothing”.

is it possible to change it to “something”?

thanks

  • 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-05-15T18:56:33+00:00Added an answer on May 15, 2026 at 6:56 pm

    You have to remember, variables in Ruby are just references to objects. Each reference is independent of any other reference, though they may refer to the same object.

    The other thing to remember is the the scope of a block is the scope it was defined in. So for your block, verb is not in scope (because it was defined outside of the method where verb lives).

    Besides the eval-binding hack mentioned by stephenjudkins, there are two ways to do what you want. One is just to assign verb:

    def test
      verb = 'nothing'
      verb = yield verb
      puts verb
    end
    

    This pretty directly does what you want and is really the best way to go about it.

    The other way is to directly mutate the object that’s passed in:

    test {|verb| verb.replace 'something'}
    

    I don’t recommend this, though, because it actually changes the string itself instead of just assigning a new string to the variable. So other places where the same string object is referenced, it will contain the new text. This isn’t a problem in your example, but it’s a massive bug waiting to happen in any real program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looks like you are releasing a UIViewController and not releasing… May 16, 2026 at 12:37 pm
  • Editorial Team
    Editorial Team added an answer Simple text: <CheckBox ToolTip="Delete"/> More control over display: <CheckBox> <CheckBox.ToolTip>… May 16, 2026 at 12:37 pm
  • Editorial Team
    Editorial Team added an answer use the ViewData and pass it to the view in… May 16, 2026 at 12:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Possible Duplicate: Is Java pass by reference? In java are the parameters passed by
Possible Duplicate: How do you pass a function as a parameter in C? Is
Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? I wonder if
Possible Duplicate: Are there gotchas using varargs with reference parameters Hi, I have a
Possible Duplicate: How do I pass parameters to the File::Find subroutine that processes each
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
Possible Duplicate: or is not valid C++ : why does this code compile ?
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
I'd like to pass a table as a parameter into a scaler UDF. I'd
Possible Duplicate: how to copy char * into a string and vice-versa I have

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.