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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:52:44+00:00 2026-06-08T06:52:44+00:00

Coming from a JavaScript background I’ve become accustomed to being able to use JavaScript’s

  • 0

Coming from a JavaScript background I’ve become accustomed to being able to use JavaScript’s dynamic scope to encapsulate values in a function. For example:

function Dog( firstname, lastname ) {

  this.fullname = firstname + lastname

  return {
    say_name: function () {
      return fullname;
    }
  }

}

Now in Ruby I’m not so sure something like this would work too well:

class Foo

  attr_accessor :bar, :baz

  def initialize bar, baz
    @bar = bar
    @baz = baz
  end

  def give_me_a_proc
    return Proc.new { @bar + @baz }
  end
end

Can anyone give a quick explanation of how scope works in Ruby? If I call the Proc returned from give_me_a_proc, will it still have access to its define-time scope?

Also do the values become fixed once I define the proc or do any changes made in Foo get carried down to the Proc even after it has been defined?

  • 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-08T06:52:45+00:00Added an answer on June 8, 2026 at 6:52 am

    In Ruby a proc is a closure. In Javascript, a function is a closure. The idea of a closure is that it “closes over” the environment in which it was defined. In Ruby the variables in the proc can still be changed, even by code living outside of the proc, and the new values will be reflected within the proc(see peakxu for demonstration). Not sure if Javascript closures work this way.

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

Sidebar

Related Questions

I'm coming to javascript from C background. In javascript, when I use the assignment
I am coming at Javascript from a classical OOP background and am having trouble
Background: I'm coming from Java background so not too familiar with Javascript. We are
Coming from a .NET background I'm use to reusing string variables for storage, so
Coming from a PHP background, I am a little spoiled with the str_replace function
Coming from a c# background, I'm probably looking at JavaScript from a completely wrong
Coming from a javascript background, collections like dictionaries are often implemented as objects because
I'm coming from a PHP/Python/Javascript background, and recently became very interested in Scala -
I'm coming from an XHTML/CSS background. I don't know much about JavaScript. Would you
I'm coming from a Javascript background which possibly is a good thing although so

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.