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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:02:26+00:00 2026-06-15T07:02:26+00:00

I was experimenting with making a DSL and ran across something that confuses me.

  • 0

I was experimenting with making a DSL and ran across something that confuses me. In my call method I wanted to set an initial value for @mymethod before evaulating the block. It works if I assign to the variable directly:

class Test

  class << self
    attr_accessor :mymethod
  end

  def self.call(&block)
    @mymethod="foo"
    class_eval &block
  end

end

Test.call do
  puts "mymethod returned: #{mymethod}"
  mymethod = "bar"
  puts "mymethod is now: #{mymethod}"
end

Which returns:

[1] pry(main)> load 'test.rb'
mymethod returned: foo
mymethod is now: bar
=> true

But I feel like this should work and it doesn’t. The only thing that has changed is the @ has been removed from the assignment to mymethod so I think it should be using the mymethod= method created by attr_accessor:

class Test

  class << self
    attr_accessor :mymethod
  end

  def self.call(&block)
    mymethod="foo"
    class_eval &block
  end

end

Test.call do
  puts "mymethod returned: #{mymethod}"
  mymethod = "bar"
  puts "mymethod is now: #{mymethod}"
end

However the assignment to mymethod from within call fails while the same assignment inside the block succeeds:

[1] pry(main)> load 'test.rb'
mymethod returned: 
mymethod is now: bar
=> true

What’s going on here? Can someone explain to my why the assignment would fail inside the call method?

  • 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-15T07:02:30+00:00Added an answer on June 15, 2026 at 7:02 am

    in your case, mymethod="foo" will define mymethod local variable
    rather than call mymethod= method.

    use self.mymethod="foo" instead

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

Sidebar

Related Questions

i've been experimenting with javascript's prototypal inheritance and have come across something that perhaps
I'm experimenting with internationalization by making a Hello World program that uses properties files
I'm experimenting with MCV using jquery. I'm making a call to an api, which
In the effort to learn more about applets and Java, I am experimenting making
I've been experimenting with making Facebook apps and I'm still not clear on app
I'm experimenting by making a social network from scratch in PHP/MySQL, but I'm having
I'm experimenting with pop-down menus (inside floating DIVs). Making them appear with onmouseover attributes
Experimenting with Scala... I'm trying to define something analogous to the @ hack in
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
I've been experimenting with making a simple Python web crawler, and I'm using regular

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.