News for hackers. Stuff that matters.
Re:How to Dynamically Create Instance Variables in Ruby by bradgessler on Friday July 28, 2023 @02:50AM (#36903745)
This technique is useful in Rails if you need to assign instance variables that have the name of a model.

Here’s an example: https://github.com/rubymonolith/demo/blob/main/app/controlle...

Re:How to Dynamically Create Instance Variables in Ruby by psychoslave on Friday July 28, 2023 @03:16AM (#36903900)
If you just need to pass data around in something more rigid than a hash, you can use a struct:

  someone = Struct.new(*%i[name age craze whatever]).new(?Ô, 128, {color: :pink}, :nevermind)

Or even an open struct for more flexibility, if performance is not critical and an additional require doesn’t matter.

https://ruby-doc.org/stdlib-3.1.0/libdoc/ostruct/rdoc/OpenSt...

Re:How to Dynamically Create Instance Variables in Ruby by joshmcmillan on Friday July 28, 2023 @06:52AM (#36905119)
Ruby 3.2 has `Data.define`, which is an even better way of doing this: https://docs.ruby-lang.org/en/3.2/Data.html
Re:How to Dynamically Create Instance Variables in Ruby by psychoslave on Friday July 28, 2023 @07:50AM (#36905530)
Awesome, thanks for pointing it.
Re:How to Dynamically Create Instance Variables in Ruby by nerdponx on Friday July 28, 2023 @08:49AM (#36906054)
Nice, it's attrs/dataclass from Python.
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 revskill on Friday July 28, 2023 @03:49AM (#36904084)
This is beautiful, but "ugly" at the same time.

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.

Re:How to Dynamically Create Instance Variables in Ruby by rcxdude on Friday July 28, 2023 @07:25AM (#36905352)
This kind of thing is possible in both python and javascript, it's just generally discouraged.
Re:How to Dynamically Create Instance Variables in Ruby by taf2 on Friday July 28, 2023 @07:40AM (#36905453)
It’s actually easier in JavaScript compared to either language….

var foo = {}

foo.bar = “hello new property”

console.log(foo.bar)

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 danielheath on Friday July 28, 2023 @09:36AM (#36906566)
On the other hand...

foo = {bar: "123"}; with (foo) eval("console.log(bar)")

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 lprtyc on Friday July 28, 2023 @07:08AM (#36905224)
Ruby websites like GitHub generally seem nicer and more elegant that Python/Django ones like python.org or Instagram. The latter are clunky.

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.

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 x86x87 on Friday July 28, 2023 @07:21AM (#36905332)
Python is almost as good as Ruby. They have slightly different takes on a few things (taaaaabs) but you can easily jump from one to the other.
Re:How to Dynamically Create Instance Variables in Ruby by bluerooibos on Friday July 28, 2023 @10:09AM (#36907039)
Almost being the key word here ;)

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.

Re:How to Dynamically Create Instance Variables in Ruby by sethherr on Friday July 28, 2023 @10:39AM (#36907470)
This! Python testing ergonomics are terrible, worse than Js.

It’s a reason I love Elixir.

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

Thanks for reading my highly technical analysis.

Re:How to Dynamically Create Instance Variables in Ruby by bbatha on Saturday July 29, 2023 @12:09PM (#36921610)
Japanese web design certainly isn’t tidy or elegant
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 ransom1538 on Friday July 28, 2023 @10:19AM (#36907164)
Or an... array? Like why bother?
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