- Joined
- Jan 1, 2020
No joke, I’m more of a sucker for Pascal. Even if it is old, the way how it’s coded looks nice to me. I’d say this and Perl are the best when learning the language as a hobby, in my opinion.
Last edited:
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Is this your brain on OOP?This is probably an elementary question for which I feel obligated to apologize, but I'm not finding an answer in any of my reference materials which is never a good sign. In short, I'm working in Ruby where I want to create a method that can change an arbitrary number of class vars for an instance of a class object by invoking the method along with two sets of params, one being the attrs to change and the other being the new values to use or formula if it's derived data. Kind of like attr_accessor or attr_writer but working on multiple, arbitrary vars so I don't have to 1. repeat myself 2. hardcode which vars are being changed. Once that's figured out I'd also like to be able to abstract it out a little further so that I can change how it's invoked in an extensible fashion.
For example if I have a People class with say, @Name, @job_title, @department, @salary, @boss, @subbordinates, @guid class vars how can I call a single method to at one time change @salary, @boss, @job_title one time, and another @Name, @guid, or the like, and then once that's done abstract it out so I can invoke say,
bob = People.new(...) these_vars_to_change = [...] engineering_vars = [...] marketing_vars = [...] which_vars_to_change = 'marketing' bob.change_many_vars(these_vars_to_change, 'which_vars_to_change'_vars) # This part would be handled by flow control. which_vars_to_change = 'engineering' bob.change_many_vars(these_vars_to_change, 'which_vars_to_change'_vars)
In both the first and second, more abstract case the issue I'm running into is that any vars I pass in are either hardcoded and therefore useless or interpreted as a string literal and therefore useless. I don't like being spoonfed, but the fact that I'm not turning up anything on what should be such an obviously essential tool means I must be missing something fundamental.
Yikes.In short, I'm working in Ruby
What, you mean something like this? e.g. creating and removing instance variables at runtimeI want to create a method that can change an arbitrary number of class vars for an instance of a class object by invoking the method along with two sets of params, one being the attrs to change and the other being the new values to use or formula if it's derived data.
my_object = Object.new
my_object.instance_variable_set(:@new_var, 'value') #=> "value"
my_object.inspect #=> "#<Object:0x00005573bffa13a0 @new_var=\"value\">"
my_object.remove_instance_variable(:@new_var) #=> "value"
my_object.inspect #=> "#<Object:0x00005573bffa13a0>"
Bruh, why would you not just use a map/dictionary at that point. What's in your software architecture that's requiring you to be able to batch-change dynamically created member variables?This is probably an elementary question for which I feel obligated to apologize, but I'm not finding an answer in any of my reference materials which is never a good sign. In short, I'm working in Ruby where I want to create a method that can change an arbitrary number of class vars for an instance of a class object by invoking the method along with two sets of params, one being the attrs to change and the other being the new values to use or formula if it's derived data. Kind of like attr_accessor or attr_writer but working on multiple, arbitrary vars so I don't have to 1. repeat myself 2. hardcode which vars are being changed. Once that's figured out I'd also like to be able to abstract it out a little further so that I can change how it's invoked in an extensible fashion.
For example if I have a People class with say, @Name, @job_title, @department, @salary, @boss, @subbordinates, @guid class vars how can I call a single method to at one time change @salary, @boss, @job_title one time, and another @Name, @guid, or the like, and then once that's done abstract it out so I can invoke say,
bob = People.new(...) these_vars_to_change = [...] engineering_vars = [...] marketing_vars = [...] which_vars_to_change = 'marketing' bob.change_many_vars(these_vars_to_change, 'which_vars_to_change'_vars) # This part would be handled by flow control. which_vars_to_change = 'engineering' bob.change_many_vars(these_vars_to_change, 'which_vars_to_change'_vars)
In both the first and second, more abstract case the issue I'm running into is that any vars I pass in are either hardcoded and therefore useless or interpreted as a string literal and therefore useless. I don't like being spoonfed, but the fact that I'm not turning up anything on what should be such an obviously essential tool means I must be missing something fundamental.
Not that bad. I don't want to dynamically add and remove variables themselves, just their values. So in the first formWhat, you mean something like this? e.g. creating and removing instance variables at runtimefucking hell man people are right about us OOP speds
And then just write a method that maps that over a given pair of arrays or a hash or somethingRuby:my_object = Object.new my_object.instance_variable_set(:@new_var, 'value') #=> "value" my_object.inspect #=> "#<Object:0x00005573bffa13a0 @new_var=\"value\">" my_object.remove_instance_variable(:@new_var) #=> "value" my_object.inspect #=> "#<Object:0x00005573bffa13a0>"
you goddamned degenerate.
class People
def initialize(name, job_title, department, salary, boss, subordinates)
@name = name
(...)
@subordinates = subordinates
end
bob = People.new("bob", "wagie", "facilities", 10, "PHB", [])
bob.job_title = "senior wagie"
bob.salary = 11
bob.change_many_vars("@job_title", "@salary", "senior wagie", 11)
bob = People.new
promotion_vars = ["@job_title", "@department", "@salary", "@boss"] # Maybe hashes are better than arrays for this, idk.
engineering_promotion = ["code monkey", "engineering", 100, "PHB"]
marketing_promotion = ["pro liar", "marketing", 100, "VP of lying"]
bob.change_many_vars(promotion_vars, engineering_promotion)
Wouldn't I still have the same problem if I did?Bruh, why would you not just use a map/dictionary at that point. What's in your software architecture that's requiring you to be able to batch-change dynamically created member variables?
Is this for serialization?
You can still useSo you can see there's no addition or deletion going on, only alteration.
instance_variable_set
though, right? e.g. something like this?class People
attr_accessor :name, :job_title, ..., :subordinates
def initialize(name=nil, job_title=nil, ..., subordinates=nil)
@name = name
@job_title = job_title
...
@subordinates = subordinates
end
def change_many_vars(vars_to_change, new_values)
vars_to_change.zip(new_values).to_h.map do |var, value|
self.instance_variable_set(var, value)
end
end
end
bob = People.new
bob.inspect #=> "#<People:0x000056469a0cc6a0 @name=nil, @job_title=nil, ..., @subordinates=nil>"
bob.job_title #=> nil
promotion_vars = ["@job_title", "@department", "@salary", "@boss"]
engineering_promotion = ["code monkey", "engineering", 100, "PHB"]
bob.change_many_vars(promotion_vars, engineering_promotion)
bob.inspect #=> "#<People:0x000056469a0cc6a0 @name=nil, @job_title=\"code monkey\", ..., @subordinates=nil>"
bob.job_title #=> "code monkey"
When I run into an issue that seems like I'm using my face as one of those ice-breaking ships they use above the Arctic Circle, I try to back off a bit and question whether the resistance I'm running into is because I'm simply conceptualizing the problem and/or solution incorrectly.So you can see there's no addition or deletion going on, only alteration. The biggest problem is that unquoted the instance vars are read as the the values themselves and therefore in the first form with bob I'm changing the "@job_title" string object to "senior wagie" rather than @job_title itself. Unquoted I'm changing "wagie" to "senior wagie", but bob.@job_title remains unchanged, still "wagie". Same problem in the second form. I could of course try to kludge around this by writing class methods hardcoded each to change an exact set of instance variables so I can call
bob.change_jobtitle_department_salary(engineering_promotion[0], engineering_promotion[1], engineering_promotion[2])
which it feels like I'm being steered towards, but I'm sure I don't have to explain why that's a terrible idea.
Wouldn't I still have the same problem if I did?
Every time I hear that this or some other language finally introduces type hinting (or annotations, or whatever you want to call them) I think to myself - what the fucking fuck? So after 20-30 years of excruciating pain they (language maintainers and toolchain developers) are FINALLY admitting that these greybeard wackos mumbling something in the corner about "type safety" might have actually had some good reasons? Well, imagine my shock!
I fucking love hearing about all these failed experiments - Lua, PHP, Python, whatever else - how they finally come to drop the pretense of dynamicness being the bestest feature no srsly guis, we're not some poo-poo "static" thing lol who wants to be "static" in a dynamic and fluid XXI century oh my god why do I have 20 GiB of stacktraces in my server log...
Obviously, some batshit insane nutjob will soon develop a new, "fully dynamic BUT SAFE (no really, trust me!)" language in a rebellion against this ossified and "static" and "obsolete" technologies. And if it gains any traction, the same story will repeat itself 20-30 years down the road.
Just as (fr)agile development methodology is rediscovered over and over, with progressively more depressing results.
Unix shell variables don't have types (or maybe it's better to say everything is a string) and let you access uninitialized variables (as inHow fucking hard is it to understand what a type is? Why does it default to allowing you to access uninitialized variables with NO warning?
echo $farts
, which just prints an empty string) so maybe they're just following its lead. Seeing as how PowerShell was trying to be a step up from Unix shells, though (from what I remember; I've never used it but I remember reading about it and thinking it had some good ideas), I guess it's too bad they didn't take the initiative to improve those sorts of things.One of the single most important requirements of the ISA is that students can implement a bare bones version of it in a semester. This plus arguments about complexity when you try to make it fast is one reason it doesn't have CPU status flags, one excuse for not making it easy to detect anything but divide by zero is that "C and Java don't care." People who want to write correct software, and the cryptography community that needs to work with long integers are not pleased.I'll tl;dw what I think are the most interesting bits: The ISA of RISC-V is very bare bones with features which are common in other chipsets, such as multiplication and division, actually implemented with extensions. While I suppose this is great for ensuring your architecture can work in dollar-store wristwatches as well as workstations and servers [you point out the ecosystem mess this can result in].
That's a reflection of its relatively immature state, ARM is 36 years old, RISC-V only 11, and it took a long time for ARM to get big and then huge. If RISC-V gets popular enough power/dollar will probably fix itself. Meanwhile a variety of major users of embedded chips like Western Digital are or have moved to it to avoid paying the "ARM tax."Also, RISC-V chips are currently not nearly competitive with ARM chips in terms of power per dollar....
Why bother to detect divide by zero? C doesn't do divisions by zero, so it won't ever happen.one excuse for not making it easy to detect anything but divide by zero is that "C and Java don't care."
This looks a lot like what I want, and thanks for saving me tens of man hours. But to a more open ended question everyone's calling me a degenerate for trying to create an interface that does attr_writer for n instance variables at a time rather than just one, which seems like a logical jump to me. Clearly I'm missing a fundamental piece of theory and if anyone can tell me what that is, or better yet name/linkdrop a book on theory or a rant from some 80s revolutionary on The Right Way that'd be appreciated. Now that I know how to catch a fish, it's probably important to figure out why everyone says the fish I'm seeking are all diseased and radioactive.You can still useinstance_variable_set
though, right? e.g. something like this?
Ruby:class People attr_accessor :name, :job_title, ..., :subordinates def initialize(name=nil, job_title=nil, ..., subordinates=nil) @name = name @job_title = job_title ... @subordinates = subordinates end def change_many_vars(vars_to_change, new_values) vars_to_change.zip(new_values).to_h.map do |var, value| self.instance_variable_set(var, value) end end end bob = People.new bob.inspect #=> "#<People:0x000056469a0cc6a0 @name=nil, @job_title=nil, ..., @subordinates=nil>" bob.job_title #=> nil promotion_vars = ["@job_title", "@department", "@salary", "@boss"] engineering_promotion = ["code monkey", "engineering", 100, "PHB"] bob.change_many_vars(promotion_vars, engineering_promotion) bob.inspect #=> "#<People:0x000056469a0cc6a0 @name=nil, @job_title=\"code monkey\", ..., @subordinates=nil>" bob.job_title #=> "code monkey"
Then you have a class method that lets you change as many of the instance variables as you want all at once (and also lets you create/overwrite private instance variables at will, so it's still degeneracy. But whatever works).
It's more the whole 'doing it dynamically at run-time' that's the issue.But to a more open ended question everyone's calling me a degenerate for trying to create an interface that does attr_writer for n instance variables at a time rather than just one, which seems like a logical jump to me.
attr_writer
for multiple attributes at a time it's easy. e.g. here's me doing it for one variable:attr_writer :one
attr_writer :one, :two, :three
class Foo
def initialize(bar)
@bar = bar
end
end
x = Foo.new(7)
x.bar #-> NoMethodError
class Foo
attr_accessor :bar
end
x.bar #=> 7
class Foo
def initialize(bar)
@bar = bar
end
end
x = Foo.new(8)
x.bar #=> NoMethodError
Foo.class_eval{attr_accessor :bar}
x.bar #=> 8
I don't know if it changes anything, but I should probably clarify that I mean condensingIt's more the whole 'doing it dynamically at run-time' that's the issue.
If you want to (statically) doattr_writer
for multiple attributes at a time it's easy. e.g. here's me doing it for one variable:
and look at me go! Here's me doing it for three:Ruby:attr_writer :one
Sure that's compile-time declaration, but why wouldn't that be sufficient? The only case I can think of where you'd want a separate interface is if you were planning on adding instance variables on-the-fly during program execution. Which is 'bad' in the sense that it kind of defeats the point of having the class-object scaffolding at all (i.e. if you knew you'd have to add those instance variables in later, then why not just add them from the start? And if your answer is that you don't know them from the start, for example because the user adds them in later, then you see the design/security issues with that too, right?)Ruby:attr_writer :one, :two, :three
Foo.one = 1
Foo.two = 'deux'
Foo.three = 11
I don't know if it changes anything, but I should probably clarify that I mean condensing
into a single statement after object Foo's already created, which I think is covered by the aforementioned instance_variable_set.Ruby:Foo.one = 1 Foo.two = 'deux' Foo.three = 11
Not to shittalk advice but isn't rails used near exclusively for the cancer that is """""web development""""" and comes with a code of conduct?In Rails there's the "update" method that does what you want. I don't think there's a way to cleanly do it on ordianry objects though. You can make a function that loops through a given hashmap and uses "instance_variable_set" on the object, but that will break any encapsulation (bypassing setters etc).
The more high-level the language is, the more trannies and web developers (but I repeat myself) it has. Only exception being the hardcore functional languages, of course.Not to shittalk advice but isn't rails used near exclusively for the cancer that is """""web development""""" and comes with a code of conduct?
Yes but a) most big software projects have a CoC now; they're practically inescapable and b) the only reason most people use Ruby nowadays is for Rails webdev, so I assumed that's what you were doing too.Not to shittalk advice but isn't rails used near exclusively for the cancer that is """""web development""""" and comes with a code of conduct?