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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:45:01+00:00 2026-05-15T13:45:01+00:00

How to simulate Java-like annotations in ruby? (Well, I have the answer, generalizing http://bens.me.uk/2009/java-style-annotations-in-ruby

  • 0

How to simulate Java-like annotations in ruby?

(Well, I have the answer, generalizing
http://bens.me.uk/2009/java-style-annotations-in-ruby)

  • 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-15T13:45:02+00:00Added an answer on May 15, 2026 at 1:45 pm

    This is adapted from a piece of code I wrote in an answer to another question a couple of weeks ago, although it is of course hardly original. This is a well-known Ruby idiom, after all, which has been in use for many years, at least since rakes‘s desc method.

    module Annotations
      def annotations(meth=nil)
        return @__annotations__[meth] if meth
        @__annotations__
      end
    
      private
    
      def method_added(m)
        (@__annotations__ ||= {})[m] = @__last_annotation__ if @__last_annotation__
        @__last_annotation__ = nil
        super
      end
    
      def method_missing(meth, *args)
        return super unless /\A_/ =~ meth
        @__last_annotation__ ||= {}
        @__last_annotation__[meth[1..-1].to_sym] = args.size == 1 ? args.first : args
      end
    end
    
    class Module
      private
    
      def annotate!
        extend Annotations
      end
    end
    

    Here’s a small example:

    class A
      annotate!
    
      _hello   color: 'red',   ancho:   23
      _goodbye color: 'green', alto:  -123
      _foobar  color: 'blew'
      def m1; end
    
      def m2; end
    
      _foobar  color: 'cyan'
      def m3; end
    end
    

    And of course no Ruby code would be complete without a testsuite:

    require 'test/unit'
    class TestAnnotations < Test::Unit::TestCase
      def test_that_m1_is_annotated_with_hello_and_has_value_red
        assert_equal 'red', A.annotations(:m1)[:hello][:color]
      end
      def test_that_m3_is_annotated_with_foobar_and_has_value_cyan
        assert_equal 'cyan', A.annotations[:m3][:foobar][:color]
      end
      def test_that_m1_is_annotated_with_goodbye
        assert A.annotations[:m1][:goodbye]
      end
      def test_that_all_annotations_are_there
        annotations = {
          m1: {
            hello:   { color: 'red',   ancho:   23 },
            goodbye: { color: 'green', alto:  -123 },
            foobar:  { color: 'blew'               }
          },
          m3: {
            foobar:  { color: 'cyan'               }
          }
        }
        assert_equal annotations, A.annotations
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Java project and need to have a keypress simulate
I have to write a program (perl , python or java) to simulate workload
I have been given an assignment to simulate an NFA in Java. Now the
I'd like to know whether it's possible to simulate Javascript prototypes in Java. Is
I have a java concurrency problem, it goes like this: There is a Servlet
Is there a way to run or simulate running Java statements (kind of like
I'm trying to simulate waiting in a java swing application, so it's basically like
Does Php have any modifiers like 'persistent' in java? My purpose is to store
I was wondering, in java, is it possible to in anyway, simulate pass by
Say I have a GUI Java project of something that simulates an ATM machine,

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.