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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:45:16+00:00 2026-06-13T01:45:16+00:00

We define a function foo: def foo(s) case s when’foo’ x = 3 puts

  • 0

We define a function foo:

def foo(s)
  case s
  when'foo'
    x = 3
    puts x.inspect
  when 'bar'
    y = 4
    puts y.inspect
  end
  puts x.inspect
  puts y.inspect
end

We then call it as follows:

1.9.3p194 :017 > foo('foo')
in foo scope
3
in outer scope
3
nil
 => nil 

1.9.3p194 :018 > foo('bar')
in bar scope
3
in outer scope
nil
3
 => nil 

Why does the function not throw an error about an unregistered local variable in either case? In the first case, the variable y seems like it should not exist, so you can’t call inspect on it in the outer scope; the same for x in the second case.

Here’s another similar example:

def test1
  x = 5 if false
  puts x.inspect
end

def test2
  puts x.inspect
end

And then:

1.9.3p194 :028 > test1
nil
 => nil 

1.9.3p194 :029 > test2
NameError: undefined local variable or method `x' for main:Object

What’s going on here? It seems like Ruby is hoisting the variable declaration into the outer scope, but I wasn’t aware that this is something Ruby does. (Searching for “ruby hoisting” only turns up results about JavaScript hoisting.)

  • 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-13T01:45:17+00:00Added an answer on June 13, 2026 at 1:45 am

    When the Ruby parser sees the sequence identifier, equal-sign, value,
    as in this expression

    x = 1
    

    it allocates space for a local variable called x. The creation of the
    variable—not the assignment of a value to it, but the internal
    creation of a variable—always takes place as a result of this kind of
    expression, even if the code isn’t executed! Consider this example:

    if false
      x = 1
    end
    p x # Output: nil
    p y # Fatal Error: y is unknown
    

    The assignment to x isn’t executed, because it’s wrapped in a failing
    conditional test. But the Ruby parser sees the sequence x = 1, from
    which it deduces that the program involves a local variable x. The
    parser doesn’t care whether x is ever assigned a value. Its job is
    just to scour the code for local variables for which space needs to
    be allocated. The result is that x inhabits a strange kind of variable limbo.
    It has been brought into being and initialized to nil.
    In that respect, it differs from a variable that has no existence at
    all; as you can see in the example, examining x gives you the value
    nil, whereas trying to inspect the non-existent variable y results
    in a fatal error. But although x exists, it has not played any role in
    the program. It exists only as an artifact of the parsing process.

    Well-Grounded Rubyist chapter 6.1.2

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

Sidebar

Related Questions

Give follow python function define: def Foo(a=0.1, b=1.6): pass when I call 'help(Foo)' ,
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
If I define: class Bar(object): @staticmethod def bar(): # code pass class Foo(Bar): #
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
In Scala (2.7), if I have this function: def foo(args: Array[String]) = for (arg
In Scala, I can define structural types as follows: type Pressable = { def
Suppose I have following code: def foo(s): A dummy function foo. For example: >>>
I have a dojo define function like this one define([ // some param ]),function(foo,
Is it possible to define custom names for methods like thing function foo() {
Using the example def foo(a): def bar(b): return a+b return bar d = {1:foo(1),

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.