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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:31:43+00:00 2026-05-29T16:31:43+00:00

I have a question about Ruby blocks. For example I have a Ruby Class:

  • 0

I have a question about Ruby blocks.
For example I have a Ruby Class:

class NewClass
  def initialize
    @a = 1
  end

  def some_method
    puts @a
  end
end

When I do something like that:

NewClass.new do |c|
  c.some_method
end

Everything is good, but is there any possibilities to do that somehow like:

NewClass.new do
  some_method
end

Any ideas?

  • 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-29T16:31:45+00:00Added an answer on May 29, 2026 at 4:31 pm

    Your current code will just ignore the block anyway since you don’t yield to it. For what you are trying to do in your first example you need the yield self idiom in initialize.

    For why you need a block variable in the first place, think about what the receiver for some_method would be in your second example. Without an explicit receiver it’s the top level main (unless this code is part of some other class of course, where that enclosing class would be self). See Dave Thomas’ blog post Changing self in Ruby (or Yehuda Katz’ post as pointed out by Niklas B. in the comments) for more info on that topic (the comments clear up the “proc invocation” part).

    Edit: all that said, this seems to work, but I prefer the yield self version and example 1:

    class NewClass
      def initialize
        @a = 1
      end
    
      def some_method
        puts "Hello: #@a"
      end
    
      def self.build(&block)
        x = self.new
        x.instance_eval(&block)
        x
      end
    end
    
    NewClass.build do
      some_method
    end
    

    This allows you to execute the block without a block variable and will return the new instance of the class for assigning to a variable etc.

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

Sidebar

Related Questions

I'm reading a book about Ruby/Rails and have a question about something simple. In
I have a question about ruby on rails and the process of assigning variables
I have a question about best practices for Ruby variable-scope using class_eval . In
I have asked a question on SuperUser about updating Ruby version in Google SketchUp
I have a question about doing a query through a few associations using Ruby
I am learning Ruby on Rails and I have a dumb question about the
I'm learning Ruby in my spare time, and I have a question about language
I have a question about Ruby arrays but it's kind of hard to describe
I have a couple questions about Ruby's methods, procedures, and blocks that strike me
Little question about Ruby error handling. I have some code that roughly resembles the

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.