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

Ask A Question

Stats

  • Questions 491k
  • Answers 491k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use gettext. It's the standard method of supporting I18N on… May 16, 2026 at 10:08 am
  • Editorial Team
    Editorial Team added an answer Unfortunately the SSRS web services do not support running data… May 16, 2026 at 10:08 am
  • Editorial Team
    Editorial Team added an answer It's because Mobile Safari on iPhone & iPod Touch does… May 16, 2026 at 10:08 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I'm writing a Java program to simulate a command line feeder, it runs DOS
I want to simulate user behavior in an java application and I want to
I was wondering, in java, is it possible to in anyway, simulate pass by
Possible Duplicates: ‘Friends’ equivalent for Java? Is there a way to simulate the C++
I have a probability problem, which I need to simulate in a reasonable amount
I'd like to have Silverlight draw the blue L and C in the image
I need to store a sparse matrix on disk. It is like a database
I was reading about Clojure and found a discussion about Java not supporting tail
I am trying to simulate the Youtube Autocomplete Search experience. I can't find 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.