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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:26:05+00:00 2026-06-08T08:26:05+00:00

I have a model that loads dynamically data from a REST API if a

  • 0

I have a model that loads dynamically data from a REST API if a property is accessed and its property has no data:

class Issue
  attr_accessor :ticket_title, :priority, :description
  @ticket_title
  @priority
  @description

  def priority
    if !@priority.empty?
      updateProperties()
    end
    @priority
  end
  def description
    if !@description.empty?
      updateProperties()
    end
    @description
  end
  def ticket_title
    if !@ticket_title.empty?
      updateProperties()
    end
    @ticket_title
  end

  def updateProperties
    # loads all data from REST API
  end

  def initialize (hsh = {})
      hsh.each { |key, value|
        self.instance_variable_set("@#{key}", value) 
      }
  end

end

There are two problems:

  1. If I initialize the model with RedmineIssue.new :ticket_title => 'test', I dont want to the model to call updateProperties, but somehow it does.
  2. Is there a way to declare functions magically? I.e., if there is no real function try to run a meta function?
  • 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-08T08:26:07+00:00Added an answer on June 8, 2026 at 8:26 am

    Well, here’s something for the “magic”. In your class, if you want to be able to get any part of a hash through a method named get_whateverthekeyis (silly example, I know), you’d use method_missing:

    class Foo
      def initialize
        @h = {}
      end
      def method_missing(name, *args, &block)
        if(name =~ /\Aget_(.+)\Z/)
          #Name is valid
          sig_class.class_exec($1) do |n|
            define_method("get_"+n) {@h[n]}
          end
    
          @h[$1]
        elsif(name =~ /\Aset_(.+)\Z/)
          #Name is valid
          sig_class.class_exec($1) do |n|
            define_method("set_"+n) {|v| @h[n]=v}
          end
    
          @h[$1] = args[0]
        else
          super
        end
      end
      private
      def sig_class
        class << self
          self
        end
      end
    end
    

    When the method is not defined, it goes to method_missing. When that happens, method_missing does the requested operation on the hash, and then dynamically defines the method, so that next time it is a direct method call, meaning that it is much more efficient because the method already exists. This is somewhat akin the the method_missing used by Ruby’s own OpenStruct.

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

Sidebar

Related Questions

I have a model that contains an Address property and Latitude / Longitude properties.
I have a Model that named word. this is my word model public class
I have a model that looks like: [Serializable] public class TemplatePageModel { public PageModel
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)
I have a model that has a ForeignKey to the built-in user model in
Would it be considered bad practice to have a viewmodel that has a property
I have UITableViewCell s that have an imageView that loads a UIImage from a
I have a section of data that loads a main view with a stylesheet
I have a model Image that has a propery named uploaded_by_user that is a
I have an intranet site that loads in IE7 compatibility mode, unless the user

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.