Robert… one thing… maybe not that nice at the end… since to assign
it we need to supply even the {} so it would look like this:
MyClass.configure({
att1: “xxx”,
att2: 3333
})
But for the moment the best solution…
Robert K. wrote in post #1169749:
Mario R. wrote in post #1169736:
In another languages it is possible to do something like:
The issue is with setter names generated with attr_accessor, ie. their
equal sign `=’ at the end. In such case Ruby can’t distinguish between
setter call and local variable assignment.
You can remove the ambiguity in several ways.
#First, possibly closest to your requirement is use of an explicit
receiver:
def with(klass)
yield klass
end
Or directly written:
World::Europe::Italy.class_exec do |c|
with World::Europe::Italy do |c|
c.food=‘fussili’
c.capital=‘rome’
c.population=2000000
c.visited=true
end
Another way is naming setter in a different way like
class World::Europe::Italy
def self.set_food(val) @att1 = val
end
end
actualy I have many attributes to supply sometimes and actually a little
more complicated since those classes are inside other classes so even it
is not nice at all… and loose of time to be repeating writing the same
classes and classes like this:
Of course these are not the real classes i have it is just an example of
more or less what i am doing… so what i am asking is if it is possible
to do something like this for the cases with many settings:
World::Europe do {
revisit=true
xtreme=false
}
World::Europe::Italy do {
food=‘fussili’
capital=‘rome’
population=2000000
visited=true
}
or something similar so it is nice to see and not necessary to repeat
again and again the classes
The ‘with’ one looks nice actually and very logical
I tried the last one you propose but… always get this error when using
send :food, ‘fusssili’
wrong number of arguments (1 for 0) (ArgumentError)
Robert… one thing… maybe not that nice at the end… since to assign
it we need to supply even the {} so it would look like this:
MyClass.configure({
att1: “xxx”,
att2: 3333
})
But it does not solve the problem the OP hat - at least as far as I
understand: the point was to initialize an instance with little clutter
(i.e. not having to repeat the name of the variable which references the
instance).
require ‘ostruct’
It’s useful.
Certainly! But you can do better if you care for shorter code: