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

  • Home
  • SEARCH
  • 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 6885317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:37:10+00:00 2026-05-27T05:37:10+00:00

I am new to Ruby and am learning from reading an already written code.

  • 0

I am new to Ruby and am learning from reading an already written code.
I encounter this code:

label = TkLabel.new(@root) do
    text 'Current Score: '   
    background 'lightblue'
end

What is the semantics of the syntax “do” above?
I played around with it and it seems like creating a TkLabel object then set its class variable text and background to be what specified in quote. However when I tried to do the same thing to a class I created, that didn’t work.

Oh yeah, also about passing hash into function, such as

object.function('argument1'=>123, 'argument2'=>321)

How do I make a function that accepts that kind of argument?

Thanks in advance

  • 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-27T05:37:11+00:00Added an answer on May 27, 2026 at 5:37 am

    What you’re looking at is commonly referred to as a DSL, or Domain Specific Language.

    At first glance it may not be clear why the code you see works, as text and background are seemingly undefined, but the trick here is that that code is actually evaluated in a scope in which they are. At it’s simplest, the code driving it might look something like this:

    class TkLabel
      def initialize(root, &block)
        @root = root
    
        if block
          # the code inside the block in your app is actually
          # evaluated in the scope of the new instance of TkLabel
          instance_eval(&block)
        end
      end
    
      def text(value)
        # set the text
      end
    
      def background(value)
        # set the background
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Rails and i'm currently learning from tutorials from http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#top find_by_email works
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
I have recently started learning C++ and coming from a Ruby environment I have
I'm learning Ruby and I like playing with irb to discover new features and
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
I'm working on learning ruby/rails at the moment - The book i'm reading has
I have this code that converts an array of date strings from a format
I'm new to ruby, learning Sinatra. While creating a Sinatra site by requiring 'sinatra'
I just started learning Ruby coming from Java. In Java you would use packages
I learning ruby singletons and have misunderstanding with such code: class MyClass def self.class_singleton_mymethod

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.