AirEaterShibainu has quit [Ping timeout: 240 seconds]
AEtherC0r3 has quit [Quit: No Ping reply in 180 seconds.]
AEtherC0r3 has joined #ruby
<havenwood>
comet23: Sounds like you want: filter(&prc)
<havenwood>
comet23: Oh, this is like a reimplement #select/#filter from Enumerable? Use #reduce or #each_with_object rather than #map.
<comet23>
there's no way to create a new key value hash like that?
<comet23>
i know in javascript you can
<havenwood>
comet23: I mean, sure, but #map is going to always return the same number of elements as the enum you called it on.
<havenwood>
comet23: Use #reduce or #each_with_object instead.
<havenwood>
comet23: Maybe start with: each_with_object({}) do
<comet23>
thank you i will mention crossposting next time i thought this channel was dead
<havenwood>
comet23: I don't want to show more or I'd give it away, but check out #each_with_object and #reduce.
<havenwood>
comet23: Alternatively, use #each and some local variables.
<comet23>
i'm using reduce... i couldn't figure out each with object i'll read the docs after my test cases pass
<comet23>
i'm trying to make things work with just my brain.. it's a tough challenge
<havenwood>
comet23: You show #map in the code above ^ not #reduce. It's fine to use #reduce but I've seen more folk get very confused by it where #each_with_object can be a bit kinder to start with.
<comet23>
yeah i just changed .map to .reduce
<comet23>
so it's self.reduce
<havenwood>
You can drop the `self`.
<comet23>
oof
<comet23>
but i'm inside a method
<havenwood>
Consider: each_with_object({}) do |(key, value), hash|
<havenwood>
comet23: Play around with it ^ and treat `hash` like you would any Hash.
<comet23>
but how am i going to iterate if i don't call it on a collection?
<havenwood>
Is `self` not a collection? I presumed it was if it responds to #map, but that's not necessarily true?
<comet23>
basically my method just accepts a proc/block
<havenwood>
If you just `each_with_object` there's an implicit `self.each_with_object`. You just don't need to type `self` everywhere.
<comet23>
&prc
<havenwood>
Right.
<comet23>
 def my_select(&prc)
<comet23>
that's what the method is
<havenwood>
Yes. Have you not implemented #each and included Enumerable?
<havenwood>
(Assuming this isn't a Hash?)
<comet23>
i have to rewrite the each method so i can't use each
<havenwood>
comet23: Sharing more code would prevent all this guessing.
<comet23>
oh i'm so sorry
<comet23>
okay
<comet23>
it's a "homework" problem so i don't really want answers
<havenwood>
comet23: So normally you first implement #each then include Enumerable to get all the Enumerable methods. If you're using #map and #reduce I assumed that was the case.
<havenwood>
comet23: If #each_with_object is available, just use that.
<havenwood>
comet23: Then port it to #reduce once you grok it.
<comet23>
you're saying i could do without self?
<comet23>
that's interesting and raises even more questions
<havenwood>
comet23: Looks like `self` inside the method is presumed to be a Hash.
<comet23>
yeah it's supposed to be a hash
<comet23>
but am i understanding you correctly that i could omit the self keyword in this case and just call reduce directly?
<havenwood>
comet23: It's just that `self.each_with_object` is an explicit `self`, but `each_with_object` implicitly is still on `self`.
<comet23>
okay i'm going to google implicit self ruby
<comet23>
thank you :)
<havenwood>
comet23: Or just leave the `self.` for now. :)
<comet23>
when do i use self
<havenwood>
comet23: I'd focus on learning and applying #each_with_object.
<havenwood>
comet23: Only when you must. You can put it around all the time, and some folk do scatter it unnecessarily, but you don't need to use it everywhere folk sometimes do.
<havenwood>
comet23: Like I could write `self.puts 42` but just write `puts 42` instead.
<comet23>
all the code examples i saw explicitly use self everywhere
<comet23>
i wish ruby wasn't so ambiguous
<comet23>
but maybe the ambiguity is good for beginners? idk i'm confused how all this magic works
<havenwood>
Python says "explicit is better than implicit" so you're not alone. No, it's not handy for beginners, it's for experts.
<havenwood>
Ruby is for expert C programmers. :P
<havenwood>
(Written by an expert C programmer.)
<havenwood>
comet23: I consider Elixir much simpler and easier to teach than Ruby or Python.
<havenwood>
comet23: Ruby is nice once you learn it well.
<comet23>
should i learn c after?
<comet23>
i just checked out a c book from my local library because i wanted to do the edx cs50 course
<havenwood>
If you want to be a systems programmer and don't like the looks of Rust or Zig, sure!
<havenwood>
comet23: Keep in mind, programming is vast and you might want to pick an emphasis area.
<havenwood>
comet23: Probably a good idea to learn one language very well, since it'll probably serve you well for all other languages.
<comet23>
maybe the blockchain would be nice
<comet23>
i think c would be a good choice but i hear rust is the most loved language of all time
<havenwood>
comet23: Consider if you want to do systems programming at all? If you do, Rust and Zig are both up and coming.
<comet23>
i want to try my hands on developing exploits some day
<havenwood>
comet23: Those can be at many levels. Ruby is fine.
<comet23>
i want to build a drone or a website... basically whatever idea i have in my head i'd be able to do with code
<havenwood>
comet23: Try Elixir if you want something straightforward but also very well done.
<comet23>
i think ruby is too well done
<comet23>
i have no other way of describing it
<havenwood>
I wouldn't mind it being more well done but would mind it being less.
<comet23>
like they thought of everything it seems
<havenwood>
comet23: Not all up front. Grown, awkwardly over time.
<havenwood>
But it's multiparadigm and will get you a long way towards understanding other languages.
<comet23>
okay i will fight through this mental frustration
<comet23>
as long as i can see a goal to work towards to it's easier for me
<Ziyan>
havenwood time to that 🙏
<Ziyan>
I was invited to a client's Sinatra project (I was originally PHP, Python, C#, VB, Java). I looked at Ruby on weekend and I couldn't go back. Ruby is so nice. I loved Python, and after like 10 years I am starting to look at Python an ML. Ruby is amazing comet23 .
<comet23>
havenwood i just got my test cases to pass... here's my final method with your suggestions about implicit stuff https://bpa.st/ZT7Q
<comet23>
Ziyan i just hate the ambiguity but ruby is a beautiful language
<comet23>
it looks clean and it works like magic but i hate magic haha
<Ziyan>
comet23 ambiguity, you mean the lack of strong types? I had that issue when I started PHP from a Java, C, C++, and VB background. You get used to it. I have done a lot of code, and very seldomly has it been a problem lately.
<Ziyan>
comet23 magic it is...you can also look at Elixir, its kinda strongly typed system, very close to Ruby, I am still learning (too busy with Rails stuff)
<comet23>
what i mean by ambiguity is leaving stuff out like implicit returns and self and other shortcuts
<comet23>
leaving it out just raises questions for me about how it knows what to do when it's not explicitly defined
jpn has joined #ruby
gr33n7007h has quit [Quit: WeeChat 3.5]
jpn has quit [Ping timeout: 246 seconds]
AEtherC0r3 has quit [Quit: No Ping reply in 180 seconds.]
AEtherC0r3 has quit [Quit: No Ping reply in 180 seconds.]
roadie has joined #ruby
AEtherC0r3 has joined #ruby
gr33n7007h has joined #ruby
ur5us has quit [Ping timeout: 240 seconds]
fercell has joined #ruby
some14u has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
some14u has joined #ruby
r3my has quit [Quit: Connection closed for inactivity]
Ziyan has joined #ruby
jpn has quit [Ping timeout: 240 seconds]
Tempesta has quit [Read error: Connection reset by peer]
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
some14u has joined #ruby
dionysus70 has joined #ruby
dionysus69 has quit [Ping timeout: 256 seconds]
dionysus70 is now known as dionysus69
Ziyan has quit [Ping timeout: 246 seconds]
Ziyan has joined #ruby
jpn has joined #ruby
libsys has quit [Ping timeout: 276 seconds]
Sheilong has joined #ruby
<adam12>
havenwood: interesting wrt Elixir and easy teaching. I find Elixir nearly unreadable when I come back to it. And if I end up with a stacktrace, all bets are off.
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
some14u has joined #ruby
<havenwood>
adam12: I guess my experience is in the context of teaching three junior devs Phoenix and Rails, so maybe more of a statement about Phoenix being explicit.
<havenwood>
adam12: But even just having Elixir module and functions be straightforward saved a ton of time compared to Ruby object model.
<adam12>
havenwood: I can see that, but it seems incredibly low-bar, which I feel once you introduced GenServer it would be the same (if not higher) complexity level.
<adam12>
So perhaps the learning curve is just stretched out a bit.
<havenwood>
adam12: What's the Ruby equivalent though of GenServer complexity? I felt like I got further before grinding to a halt, hah.
<adam12>
havenwood: Instance of a class, imho.
<havenwood>
Probably, and that's something you've gotta use way before GenServers.
<adam12>
I was very in favour of Elixir a long while back, but if anything is going to unseat Ruby for me, it would probably be something Go like. Dialyzer isn't enough.
<havenwood>
adam12: I guess I'd rather teach Roda than Plug though.
<havenwood>
adam12: I'd prefer even Swift to Go. I'm not so much on the Go train, just aesthetically.
<adam12>
havenwood: I actually rewrote a Go microservice in Ruby not too long ago. I ended up in Go Module hell and it just wasn't worth it. I spent 2 days fighting Go Module hell and 2 days just rewriting it in Ruby, with more features.
<havenwood>
Ruby ftw!
<adam12>
Everyone is all the rage about types, but I usually test everything anyways, so types offer me very little other than at the boundaries.
<adam12>
Which is where I'm using dry-types or dry-schema/dry-validation anyways.
dionysus70 has joined #ruby
dionysus69 has quit [Ping timeout: 246 seconds]
dionysus70 is now known as dionysus69
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
some14u has joined #ruby
some14u has quit [Client Quit]
some14u has joined #ruby
<adam12>
I think I've said all that before in here, since I'm having serious deja vu.
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
some14u has joined #ruby
bit4bit has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
howdoi has joined #ruby
Ziyan has quit [Ping timeout: 276 seconds]
Ziyan has joined #ruby
some14u has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
some14u has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
oxfuxxx has quit [Ping timeout: 240 seconds]
AEtherC0r3 has quit [Quit: No Ping reply in 180 seconds.]
AEtherC0r3 has joined #ruby
<gschanuel>
ok.. I really don't know how to achieve what I need... I need to create a hash (to output as json) with the content of event['windows']['perfmon']['metrics'], splitting the keys by '_' and creating new objects
<adam12>
gschanuel: So you want a NEW object? Or the existing JSON modified?
<gschanuel>
modify the existing json
<gschanuel>
I also need to "group" the keys
some14u has joined #ruby
Jonopoly has quit [Quit: WeeChat 3.0]
<gschanuel>
as in {"dra":{"inbound":{"values":{"dns":{"only":{"sec":10}}}}}} {"dra":{"outbound":{"values":{"dns":{"only":{"sec":10}}}}} becoming {"dra":{"inbound":{"values":{"dns":{"only":{"sec":10}}}},"outbound":{"values":{"dns":{"only":{"sec":10}}}}}}
reset has joined #ruby
<gschanuel>
I tried to start with baby steps.. merging two hashes but the content are replaced and only one is kept
<gschanuel>
I know nothing about ruby yet.. just need this task
<adam12>
gschanuel: It's indepth and I have a meeting shortly.
<adam12>
gschanuel: But my suggestion is to just convert your key to the Hash, with a single element.
mbrndtgn has joined #ruby
<adam12>
gschanuel: There's a few tricks you can use. Hashes can have a default proc value, and that can be recursive.
<adam12>
gschanuel: So if you can create the recursive Hash, then you could probably split and reduce, then set the value. It might be worth exploring.
<adam12>
(start by converting your key to the Hash, then move on from there)
<gschanuel>
I was able to convert the keys to hash, the "move from there" is being my problem :-P
AEtherC0r3 has quit [Quit: No Ping reply in 180 seconds.]
AEtherC0r3 has joined #ruby
akochi[m] has quit [Quit: You have been kicked for being idle]
oxfuxxx has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
TomyWork has quit [Quit: Leaving]
hololeap has quit [Ping timeout: 240 seconds]
Vonter has quit [Quit: WeeChat 3.5]
hololeap has joined #ruby
howdoi has quit [Quit: Connection closed for inactivity]
some14u has joined #ruby
jpn has quit [Ping timeout: 250 seconds]
Thanzex has quit [Read error: Connection reset by peer]
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
Thanzex has joined #ruby
ralu1 has quit [Read error: Connection reset by peer]
ralu1 has joined #ruby
oxfuxxx has quit [Ping timeout: 246 seconds]
bit4bit has quit [Ping timeout: 272 seconds]
<gschanuel>
adam12, i'm not in a hurry.. if you could give more advice later I'll be grateful
<havenwood>
gschanuel: I tried what adam12 suggested and works for me.
<havenwood>
gschanuel: map to the key and value, the reduce the split and reversed key with value as the argument, and in the reduce block return a hash.
<havenwood>
gschanuel: do you want spoilers or is that enough to go on?
<havenwood>
then* reduce the split
<gschanuel>
spoilers please! i never coded in ruby before today
<havenwood>
gschanuel: I treated your keys as Strings, but you could switch to Symbols easy enough.
<havenwood>
gschanuel: Good luck!
<gschanuel>
nice!
<gschanuel>
thanks havenwood
<gschanuel>
now I just need to figure how to "merge" same keys without replacing them :-)
<havenwood>
gschanuel: check out Hash#merge with a block.
<havenwood>
gschanuel: You can reduce your new Array of Hashes, while merging them. Also not straightforward but now I'm the one who's gotta run. Maybe create a gist with this new result and where you'd like to get to from here?
<gschanuel>
no problem, I'll read about hash#merge with block and see where I can get ;-)
<gschanuel>
you really helped a lot!! thanks
John_Ivan_ has joined #ruby
John_Ivan__ has quit [Ping timeout: 246 seconds]
danjo00 has quit [Ping timeout: 260 seconds]
some14u has joined #ruby
some14u has quit [Client Quit]
danjo00 has joined #ruby
_ht has joined #ruby
danjo00 has quit [Quit: danjo00]
danjo00 has joined #ruby
danjo00 has quit [Client Quit]
danjo00 has joined #ruby
hololeap has quit [Ping timeout: 240 seconds]
hololeap has joined #ruby
AEtherC0r3 has quit [Quit: No Ping reply in 180 seconds.]
AEtherC0r3 has joined #ruby
noname has joined #ruby
danjo00 has quit [Quit: danjo00]
roadie has quit [Ping timeout: 260 seconds]
<havenwood>
gschanuel: Or you could check out #deep_merge from Rails or the various gems that provide it too.
<ox1eef>
to do what, exactly?
danjo00 has joined #ruby
<havenwood>
Ruby doesn't ship with #deep_merge or #deep_freeze but I guess now we have Ractor.make_shareable for the latter.
<ox1eef>
a recursive Hash initialization?
<gschanuel>
yeah, I was looking exactly into deep_merge now, but this code will run on logstash and I don't believe it can handle rails
<havenwood>
gschanuel: You can cherry pick just that method from ActiveRecord.
<havenwood>
gschanuel: Or use a standalone gem that provides the same.
<havenwood>
ox1eef: It's a deeply nested hash with a bunch of repeated keys at various levels.
roadie has joined #ruby
<ox1eef>
yea i think i understood, i'm trying to write it as a recusrsive function