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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:24:35+00:00 2026-05-27T04:24:35+00:00

Trying Ruby with the help of Ruby Koans . There is following test there:

  • 0

Trying Ruby with the help of Ruby Koans. There is following test there:

def test_method_names_become_symbols
  symbols_as_strings = Symbol.all_symbols.map { |x| x.to_s }
  assert_equal __, symbols_as_strings.include?("test_method_names_become_symbols")
end

# THINK ABOUT IT:
#
# Why do we convert the list of symbols to strings and then compare
# against the string value rather than against symbols?

I tried to do the same thing in the irb console and it returned false for the undefined method. But then I tried the same in some test.rb file and true was returned to both existing and unexisting methods.

Sample code:

def test_method
end
symbols = Symbol.all_symbols.map { |x| x }
puts symbols.include?(:test_method) # returns true in both cases
puts symbols.include?(:test_method_nonexistant) # returns false in irb, true if executed directly

The questions are: why do we convert symbols to strings in such case and why there are different results in irb and normal file?

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-27T04:24:36+00:00Added an answer on May 27, 2026 at 4:24 am

    Let us go through a slightly modified version of your test code as it is seen by irb and as a stand alone script:

    def test_method;end
    symbols = Symbol.all_symbols # This is already a "fixed" array, no need for map
    puts symbols.include?(:test_method)
    puts symbols.include?('test_method_nonexistent'.to_sym)
    puts symbols.include?(:test_method_nonexistent)
    eval 'puts symbols.include?(:really_not_there)'
    

    When you try this in irb, each line will be parsed and evaluated before the next line. When you hit the second line, symbols will contain :test_method because def test_method;end has already been evaluated. But, the :test_method_nonexistent symbol hasn’t been seen anywhere when we hit line 2 so lines 4 and 5 will say “false”. Line 6 will, of course, give us another false because :really_not_there doesn’t exist until after eval returns. So irb says this:

    true
    false
    false
    false
    

    If you run this as a Ruby script, things happen in a slightly different order. First Ruby will parse the script into an internal format that the Ruby VM understands and then it goes back to the first line and starts executing the script. When the script is being parsed, the :test_method symbol will exist after the first line is parsed and :test_method_nonexistent will exist after the fifth line has been parsed; so, before the script runs, two of the symbols we’re interested in are known. When we hit line six, Ruby just sees an eval and a string but it doesn’t yet know that the eval cause a symbol to come into existence.

    Now we have two of our symbols (:test_method and :test_method_nonexistent) and a simple string that, when fed to eval, will create a symbol (:really_not_there). Then we go back to the beginning and the VM starts running code. When we run line 2 and cache our symbols array, both :test_method and :test_method_nonexistent will exist and appear in the symbols array because the parser created them. So lines 3 through 5:

    puts symbols.include?(:test_method)
    puts symbols.include?('test_method_nonexistent'.to_sym)
    puts symbols.include?(:test_method_nonexistent)
    

    will print “true”. Then we hit line 6:

    eval 'puts symbols.include?(:really_not_there)'
    

    and “false” is printed because :really_not_there is created by the eval at run-time rather than during parsing. The result is that Ruby says:

    true
    true
    true
    false
    

    If we add this at the end:

    symbols = Symbol.all_symbols
    puts symbols.include?('really_not_there'.to_sym)
    

    Then we’ll get another “true” out of both irb and the stand-alone script because eval will have created :really_not_there and we will have grabbed a fresh copy of the symbol list.

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

Sidebar

Related Questions

I am trying to install ruby-mp3info. I have the following URL from which I
I'm trying to enable Ruby via the plugin manager, and I receive the following
I am now trying to learn ruby-debug gem, but there are many jargons I
I'm trying to load up rails in the test environment using a ruby script.
I'm trying to lookup help for Ruby methods in Textmate through the Ctrl+H shortcut,
I'm trying to install Ruby on Rails using RVM. I am following this guide
I'm trying to install the following ruby gem on my mac: http://www.sinatrarb.com/intro.html I get
I'm trying to test a case in our Ruby on Rails system where we
I get this error when trying ruby script/console Rails requires RubyGems >= . Please
I was trying in Ruby on Rails how to find a prime number. Here

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.