Re:How to Dynamically Create Instance Variables in Ruby
by nerdponx on Friday July 28, 2023 @08:52AM
(#36906076)
This seems infinitely better than dynamically adding instance attributes. Dynamic instance attributes was absolutely the worst thing about "old school" Python programming. The language, community, and ecosystem are much better off now that it's considered bad style and is strongly discouraged. Unfortunately we are stuck with it in the stdlib logging framework and the Tornado web framework, but it's mercifully absent just about everywhere else, and help platforms will almost universally discourage people from doing it.
Re:How to Dynamically Create Instance Variables in Ruby
by rubyn00bie on Friday July 28, 2023 @08:14AM
(#36905737)
Your example is not equivalent to the Ruby code. You’re just assigning a value to a property which is always exposed, you’re showing getters/setters. The instance variables are not by default exposed or accessible outside the class/object.
Re:How to Dynamically Create Instance Variables in Ruby
by cpfohl on Friday July 28, 2023 @06:27AM
(#36904970)
> No such shorthand exists in Ruby (and I'm glad that Ruby doesn't have the feature bloat like TypeScript and C#)
I find that while Ruby’s syntax may have a smaller surface area there are so many more ways to do things than with other languages that I reach for others first.
Also Ruby encourages so much metaprogramming that it can be very very difficult to work out where a behavior comes from. You _really_ have to document everything and getting a good editor config for Ruby is very important. That said I still struggle to get basic autocomplete working with Ruby in vs code
Re:How to Dynamically Create Instance Variables in Ruby
by sethrin on Friday July 28, 2023 @09:32AM
(#36906519)
I use ruby-lsp and solargraph and copilot. Personally I'm rarely tempted to get very meta at work, but side projects are another thing entirely. I think one project has six DSLs?
Re:How to Dynamically Create Instance Variables in Ruby
by dlisboa on Friday July 28, 2023 @07:37AM
(#36905436)
> I wonder if the language influences the aesthetics.
The community does. You’ll notice that JavaScript command line tools are much fancier than other languages, with emojis and cool spinners. Being that many JS programmers are web developers there’s a sense of aesthetics baked in the community. Other communities don’t have to care about “pretty” in their day-to-day so their tools reflect it.
Re:How to Dynamically Create Instance Variables in Ruby
by IshKebab on Friday July 28, 2023 @07:38AM
(#36905446)
Probably just selection bias. Ruby is way more niche than Python so Ruby programmers are less likely to be complete beginners.
It's quite a bit problem for Python actually IMO. It is so popular among beginners that a lot of libraries and StackOverflow answers are beginner-quality.
Re:How to Dynamically Create Instance Variables in Ruby
by 3cats-in-a-coat on Friday July 28, 2023 @07:45AM
(#36905484)
The languages don't directly influence the aesthetics. Rather, there's a culture that precedes a language, and the audience it attracts, and it defines the marketing of that language, among other things.
There's always mutual interaction and feedback. I'm just saying I don't see this as the high order bit.
In the case of Ruby, in fact the culture didn't come from Ruby, but Ruby on Rails, which marketed almost exclusively to non-programmers. "Classic" programmers suck at design.
Re:How to Dynamically Create Instance Variables in Ruby
by matt_s on Friday July 28, 2023 @08:13AM
(#36905730)
I mean if we talk about the creators/inspiration around these languages we have creators of Ruby from Japan where things are tidy, elegant and mindful of others. An inspiration for the name of Python is from Monty Python creators of the Ministry of Silly Walks which is kinda clunky looking when you watch it. https://www.youtube.com/watch?v=eCLp7zodUiI
Re:How to Dynamically Create Instance Variables in Ruby
by bluerooibos on Friday July 28, 2023 @10:12AM
(#36907072)
Not really a problem I've come across but I'd say if you're passing 100 arguments to your initialize method, you probably need to refactor somewhere? Personally, I like seeing the instance variables I have rather than having them hidden in a magic list of attributes.
Re:How to Dynamically Create Instance Variables in Ruby
by nico on Friday July 28, 2023 @10:26AM
(#36907268)
> Disclaimer: The point of the post is not that we should all be creating instance variables dynamically from now on, but just to show one way to do it for those rare cases when you actually do need it
Make sure you absolutely need it
Trying to track variable names and finding specific code when they are being generated dynamically can quickly become nightmare
Here’s an example: https://github.com/rubymonolith/demo/blob/main/app/controlle...
https://ruby-doc.org/stdlib-3.1.0/libdoc/ostruct/rdoc/OpenSt...
For example, it allows you to monkey patch at runtime any library that you install.
Compare with Javascript, i'm not sure if you can just install a package, patch it at runtime (like this article is about) to match your need.
In some sense, this is similar to OOP inheritance and overloading, but for intance variables.
That's why most of Ruby startups tends to go faster than other languages, because all Ruby classes is hackable at runtime.
var foo = {}
foo.bar = “hello new property”
console.log(foo.bar)
foo = {bar: "123"}; with (foo) eval("console.log(bar)")
I find that while Ruby’s syntax may have a smaller surface area there are so many more ways to do things than with other languages that I reach for others first.
Also Ruby encourages so much metaprogramming that it can be very very difficult to work out where a behavior comes from. You _really_ have to document everything and getting a good editor config for Ruby is very important. That said I still struggle to get basic autocomplete working with Ruby in vs code
I wonder if the language influences the aesthetics. Python is boring and clunky as well.
Some people here criticize meta-programming. I was one of them until I read the gorgeous Homebrew configuration files. Of course, don't overdo it.
The community does. You’ll notice that JavaScript command line tools are much fancier than other languages, with emojis and cool spinners. Being that many JS programmers are web developers there’s a sense of aesthetics baked in the community. Other communities don’t have to care about “pretty” in their day-to-day so their tools reflect it.
I tried testing in Python years ago and found the likes of PyTest etc to be light years behind what RSpec and even Minitest offer.
It’s a reason I love Elixir.
It's quite a bit problem for Python actually IMO. It is so popular among beginners that a lot of libraries and StackOverflow answers are beginner-quality.
There's always mutual interaction and feedback. I'm just saying I don't see this as the high order bit.
In the case of Ruby, in fact the culture didn't come from Ruby, but Ruby on Rails, which marketed almost exclusively to non-programmers. "Classic" programmers suck at design.
Thanks for reading my highly technical analysis.
Make sure you absolutely need it
Trying to track variable names and finding specific code when they are being generated dynamically can quickly become nightmare