Ruby: Dynamic setter with inheritance

I'm trying to format hex strings using an inherited format method. I'm a bit of a Ruby-noob, any help is appreciated.

class Bar def alter_the_variable attribute_name attribute = method(attribute_name) formatted = "%.4x" % puts formatted # => 00f3 attribute = formatted # What I "want" to be able to do, but # doesn't work because attribute is a local variable #attribute.owner.send(:h=, formatted) # Doesn't work either, gives: # in `send': undefined method `h=' for Foo:Class (NoMethodError) end end class Foo < Bar def initialize @h = "f3" end def h @h end def h= val @h = val end end f = puts f.h # => f3 f.alter_the_variable :h puts f.h # => f3

-------------Problems Reply------------

Here's one way to do what you wanted to do:

def alter_the_variable attribute_name
current_value = send(attribute_name)
formatted_value = "%.4x" % current_value.hex
send (attribute_name.to_s+'=').to_sym, formatted_value

Category:ruby Views:0 Time:2011-06-23

Related post

  • Ruby - dynamically add property to class (at runtime) 2010-06-25

    This question already has an answer here: adding attributes to a Ruby object dynamically 5 answers I'm looking for a way to add properties to my already defined class at runtime, or better: class Client attr_accessor :login, :password def initialize

  • ruby dynamic method passing a value 2010-06-23

    How do I pass a value to a dynamic method name that's called on a rails model? @model.send(dynamic_method_name.to_sym,123) This gives the error: wrong number of arguments (1 for 0) If I were to use the same method like this though: @model.post_id = 1

  • Dynamic dispatch and inheritance in python 2011-01-09

    I'm trying to modify Guido's multimethod (dynamic dispatch code): to handle inheritance and possibly out of order arguments. e.g. (inheritance problem) class A(object): pass class B(A): pass @m

  • Ruby: Dynamically defining classes based on user input 2011-02-25

    I'm creating a library in Ruby that allows the user to access an external API. That API can be accessed via either a SOAP or a REST API. I would like to support both. I've started by defining the necessary objects in different modules. For example: s

  • Ruby: dynamically fill associative array 2011-05-30

    Something like that: arr=[] arr[some variable] << string How to accomplish that on Ruby? Thanks ;) --------------Solutions------------- In Ruby a Hash can be treated as an associative array. # Initialize the hash. noises = {} # => {} # Add i

  • Ruby Dynamic Classes. How to fix "warning: class variable access from toplevel" 2011-11-04

    I'm trying to write a program that dynamically defines ruby classes based on configuration read from a file. I know I can use to do this. Here's an example program: x = [1,2,3] Test = do @@mylist = x def foo puts @@mylist end end

  • Ruby dynamically tied to table 2011-11-30

    I've got a huge monster of a database (Okay that's not quite true, but there are over 8 million records in one product table).. This table is fed by 13 suppliers. Even with the best indexing I could come up with, searching for the top 10,000 records

  • Dynamic memory and inherited structs in C++ 2010-02-18

    Say I have some structs like this: struct A{ int someInt; } struct B : public A{ int someInt; int someOtherInt; } And a class: class C{ A *someAs; void myFunc(A *someMoreAs){ delete [] someMoreAs; } } would this cause a problem: B *b=new B[10]; C c;

  • Ruby metaprogramming, defining multiple "inherited" functions 2010-09-09

    I want the following module to be included in a class I have: module InheritanceEnumerator def self.included(klass) klass.instance_eval do instance_variable_set('@subclasses',[]) def self.subclasses @subclasses end original_method = self.respond_to?(

  • ruby dynamic chaining methods 2010-12-09

    HI I try to build some dynamic defined methods and chain some scope methods something like: define_method "#{instance_name_method}" do Kernel.const_get(model_name).___some_chaining methods basd on condition end One idea for that is something like: me

  • Help a ruby noob understand class inheritance 2011-01-27

    I'm trying to learn ruby by building a basic Campfire bot to screw around with at work. I've gotten pretty far (it works!) and learned a lot (it works!), but now I'm trying to make it a bit more complex by separating the actions to be performed out i

  • Can I switch versions of Ruby dynamically using RVM? 2012-02-01

    I'm working on building a GUI for my checkers implementation. I'm utilizing a GUI framework that only works with Ruby 1.8, yet my game engine, which I have packaged as a gem, only works with Ruby 1.9. I can't change the code for the GUI framework and

  • Ruby: Dynamically calling available methods raising undefined method (metaprogramming) 2010-04-21

    I have an Activerecord object called Foo: Foo.attribute_names.each do |attribute| puts Foo.find(:all)[0].method(attribute.to_sym).call end Here I'm calling all attributes on this model (ie, querying for each column value). However, sometimes, I'll ge

  • Ruby dynamic generation of strings .. then evaluating that string 2011-05-27

    Here's the setup: returns a nodeset ( multiple values ) which i loop through with .each do |l| langHash = hash with lots of 2 lette language codes 'en', 'fr' etc.. All I want to do is get XXXXXXXXXXXX to evaluate as 'English', 'French' But

  • Multiple Inheritance in Ruby? 2012-04-20

    I thought that Ruby only allowed single inheritance besides mixin. However, when I have class Square that inherits class Thing, Thing in turn inherits Object by default. class Thing end class Square < Thing end Doesn't this represent multiple inhe

  • ruby/datamapper: Refactor class methods to module 2010-05-24

    i've the following code and tried the whole day to refactor the class methods to a sperate module to share the functionality with all of my model classes. Code ( class Merchant include DataMapper::Resource property :id, Seri

  • Why use inheritance at all? 2010-07-28

    I know the question has been discussed before, but it seems always under the assumption that inheritance is at least sometimes preferable to composition. I'd like to challenge that assumption in hopes of gaining some understanding. My question is thi

  • Is ruby strongly or weakly typed? 2009-02-06

    Is ruby strongly or weakly typed ? Presumably the same is true for Javascript. --------------Solutions------------- Ruby is "strong typed". Strong typing means an object's type (not in the OOP sense, but in a general sense) is checked before an opera

  • How are Ruby and Rails related? 2010-02-01

    First of all, i want some clarification :) 1) Is RoR like a server scripting language? 2) Can it be used as an alternative to php? I started learning Ruby from The Ruby Programming Language book and haven't yet come across something which relates to

Copyright (C), All Rights Reserved.

processed in 0.139 (s). 11 q(s)