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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:42:16+00:00 2026-05-31T19:42:16+00:00

I am trying to understand how CoffeeScript variables are scoped. According to the documentation:

  • 0

I am trying to understand how CoffeeScript variables are scoped.
According to the documentation:

This behavior is effectively identical to Ruby’s scope for local
variables.

However, I found out that it works differently.

In CoffeeScript

a = 1
changeValue = -> a = 3
changeValue()
console.log "a: #{a}" #This displays 3

In Ruby

a = 1
def f
  a = 3
end
puts a #This displays 1

Can somebody explain it, please?

  • 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-31T19:42:17+00:00Added an answer on May 31, 2026 at 7:42 pm

    Ruby’s local variables (starting with [a-z_]) are really local to the block they are declared in. So the Ruby behavior you posted is normal.

    In your Coffee example, you have a closure referencing a. It’s not a function declaration.

    In your Ruby example, you don’t have a closure but a function declaration. This is different. The Ruby equivalent to your Coffee is:

    a = 1
    changeValue = lambda do
       a = 3
    end
    changeValue()
    

    In closures, local variables present when the block is declared are still accessible when the block is executed. This is (one of the) powers of closures!

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

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand how CoffeeScript instance and class variable works I came with this
I've been trying to understand how Ruby blocks work, and to do that I've
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is
Trying to understand why this doesn't work. I keep getting the following errors: left
Trying to understand this binding process of the WPF. See the code at the
I'm trying to convert this code from Javascript to CoffeeScript: for (var i =
Trying to understand this open source app on github, it has a gem file:
just trying to understand ObjectiveC a bit better. Shouldn't this two expressions be the
Trying to understand the relationship between UIView and CALayer. I read Apple documentation but

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.