adam12 changed the topic of #ruby to: Rules: https://ruby-community.com | Ruby 3.1.2, 3.0.4, 2.7.6: https://www.ruby-lang.org | Paste 4+ lines to: https://gist.github.com | Books: https://goo.gl/wpGhoQ | Logs: https://libera.irclog.whitequark.org/ruby/
va5c0 has joined #ruby
peirik__ has joined #ruby
peirik__ has quit [Ping timeout: 256 seconds]
ur5us has quit [Ping timeout: 260 seconds]
supay has quit [Quit: Connection closed for inactivity]
peirik__ has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
boey89 has quit [Ping timeout: 256 seconds]
Aminda has joined #ruby
lucasb has quit [Quit: Connection closed for inactivity]
va5c0 has quit [Quit: WeeChat 3.7.1]
markong has quit [Ping timeout: 256 seconds]
va5c0 has joined #ruby
va5c0 has quit [Quit: WeeChat 3.7.1]
John_Ivan__ has joined #ruby
John_Ivan_ has quit [Ping timeout: 256 seconds]
va5c0 has joined #ruby
cartdrige has joined #ruby
Aminda has quit [Remote host closed the connection]
Aminda has joined #ruby
va5c0 has quit [Quit: WeeChat 3.7.1]
peirik__ has quit [Ping timeout: 260 seconds]
dviola has left #ruby [WeeChat 3.7.1]
dviola has joined #ruby
KaitoDaumoto has quit [Read error: Connection reset by peer]
wand has quit [Remote host closed the connection]
wand has joined #ruby
donofrio has joined #ruby
donofrio_ has quit [Ping timeout: 252 seconds]
crax23 has joined #ruby
cartdrige has quit [Ping timeout: 260 seconds]
crax23 has quit [Ping timeout: 268 seconds]
va5c0 has joined #ruby
goldfish has quit [Ping timeout: 260 seconds]
va5c0 has quit [Quit: WeeChat 3.7.1]
Rounin has quit [Ping timeout: 268 seconds]
cartdrige has joined #ruby
goepsilongo has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
goepsilongo has joined #ruby
reset has quit [Quit: reset]
CrazyEddy has joined #ruby
cek has quit [Quit: Connection closed for inactivity]
cartdrige has quit [Quit: Leaving]
phenom has quit [Read error: Connection reset by peer]
phenom has joined #ruby
D_A_N has quit [Quit: leaving]
Sankalp has quit [Ping timeout: 256 seconds]
Sankalp- has joined #ruby
Sankalp- is now known as Sankalp
okivz has joined #ruby
<okivz> I got a 'bus error' whilst trying to (json) encode a 'circular' array.
<okivz> is there anyway of detecting 'circular' array: for example; a=[]; a<<a
<okivz> when encoding to JSON
peirik__ has joined #ruby
<okivz> is there anyway of detecting 'circular' array: for example; a=[]; a<<a, to avoid 'bus error'
<leftylink> there surely must be a way of detecting one. for example, `a.include?(a)` is true
<okivz> yep, that works, thanks
okivz has quit [Remote host closed the connection]
wand has quit [Remote host closed the connection]
wand has joined #ruby
peirik__ has quit [Ping timeout: 252 seconds]
dionysus69 has joined #ruby
jvalleroy has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
jvalleroy has joined #ruby
nmollerup has joined #ruby
Sankalp has quit [Ping timeout: 260 seconds]
Sankalp has joined #ruby
mahlon has quit [Ping timeout: 248 seconds]
_ht has joined #ruby
mahlon has joined #ruby
Rounin has joined #ruby
Rounin has quit [Changing host]
Rounin has joined #ruby
Aminda has quit [Remote host closed the connection]
Aminda has joined #ruby
Aminda has quit [Remote host closed the connection]
Aminda has joined #ruby
reset has joined #ruby
rvalue has quit [Read error: Connection reset by peer]
peirik__ has joined #ruby
rvalue has joined #ruby
peirik__ has quit [Ping timeout: 268 seconds]
<meuk> Anyone that can help me out? I'm just learning Ruby amd just learned about unit testing so I made something very simple: https://paste.sr.ht/~unknowndeveloper/3b847aae253a2a60274463ea205631c43064447a
cryptkeeper has quit [Quit: Connection closed for inactivity]
<meuk> Question is why does the test for lowercase passes while I feed it a string with capitalized characters?
<meuk> I initialize the class with .downcase on a string but removing that still makes the test pass.
<meuk> So, I'm scratching my head here
__ht has joined #ruby
_ht has quit [Ping timeout: 260 seconds]
_ht has joined #ruby
__ht has quit [Ping timeout: 256 seconds]
z4kz has quit [Ping timeout: 260 seconds]
cek has joined #ruby
cek has left #ruby [#ruby]
markong has joined #ruby
<leftylink> there are no assertions in `it "is written in lowercase"` at all. it is not testing anything
cartdrige has joined #ruby
<leftylink> so since it is not testing anything, it will always pass no matter what
<meuk> Hmm, let me look into that. Thanks leftylink ^^
cartdrige has quit [Quit: Leaving]
<meuk> Right, I forgot the .should method and assigned the object instead of comparing it >.<
goepsilongo has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
goepsilongo has joined #ruby
dionysus69 has quit [Ping timeout: 260 seconds]
<leah2> oops :)
<meuk> Hehe, yes
<leah2> good argument against magic methods
<leah2> (i repent)
<meuk> Can you eloborate on that? :)
c10l has quit [Quit: See ya! o/]
c10l has joined #ruby
alexherbo2 has joined #ruby
peirik__ has joined #ruby
cartdrige has joined #ruby
<leah2> ages ago i wrote a testing library called bacon that also used this (2+3).should == 5 style
<leah2> which is cute, but has it's issues too
<leah2> but less verbose than rspec at least :D
<leah2> one should add some code to make a warning if a test case has no assertions
c10l has quit [Quit: See ya! o/]
<meuk> Ah yes
gr33n7001 has joined #ruby
gr33n7007h is now known as Guest335
Guest335 has quit [Killed (platinum.libera.chat (Nickname regained by services))]
gr33n7001 is now known as gr33n7007h
c10l has joined #ruby
Guest335 has joined #ruby
crax23 has joined #ruby
cartdrige has quit [Ping timeout: 256 seconds]
Y05hito__ has joined #ruby
cartdrige has joined #ruby
crax23 has quit [Ping timeout: 252 seconds]
mineflaptop has joined #ruby
Y05hito__ has quit [Ping timeout: 240 seconds]
FullMetalStacker has joined #ruby
szkl has joined #ruby
mineflaptop has quit [Ping timeout: 268 seconds]
<FullMetalStacker> Hi all! I am stuck with the following exercise:
<FullMetalStacker> "Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements."
<FullMetalStacker> arr.each_with_index { |n, i| (arr.delete_at(i) and arr << 0) if n.to_i == 0 }
<FullMetalStacker> My solution so far is this:
<FullMetalStacker> It works well with e.g. this arr = [1, 2, 0, 1, 0, 1, 0, 3, 0, 1]
<FullMetalStacker> But with this array I have a strange problem:
<FullMetalStacker> [9, 0.0, 0, 9, 1, 2, 0, 1, 0, 1, 0.0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
<FullMetalStacker> Instead of the expected one:
<FullMetalStacker> [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
<FullMetalStacker> I get this one:
<FullMetalStacker> [9, 0, 9, 1, 2, 1, 1, 3, 1, 9, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0]
<FullMetalStacker> So obviously there seems to be a problem with the float on index 1.
<FullMetalStacker> This is strange to me because there is no problem with the float on index 10!
<FullMetalStacker> Also I have tried various things to also take care of floats, which all don't provide any cure, e.g. adding at the end "if n == 0 or n == 0.0 } or as I have it now, turn the n to integer first.
<FullMetalStacker> What am I doing wrong can someone help me out?
<FullMetalStacker> dfdfgdsfg
mineflaptop has joined #ruby
mineflaptop has quit [Remote host closed the connection]
D_A_N has joined #ruby
<wnd> I don't really understand what it is you're trying to do, but deleting elements of an array by index while you're interating over it (with an index) sounds like asking for trouble
cartdrige has quit [Ping timeout: 252 seconds]
<FullMetalStacker> hi wnd! Thank you for that hint, I will try to solve it with creating a new array and see if that solves my issues!
<wnd> ah, you're moving the zeros (and non-numbers) to the end of the array
dionysus69 has joined #ruby
<FullMetalStacker> exactly, see the mission statement in line 2
<wnd> ah, I'm just being blind
<FullMetalStacker> np :)
<gr33n7007h> FullMetalStacker: arr.group_by(&:zero?).values.sum([])
<gr33n7007h> god knows how efficient that is though, lol
peirik__ has quit [Ping timeout: 260 seconds]
Vonter has quit [Ping timeout: 256 seconds]
<wnd> or Array#partition { |v| !v.zero? }.flatten, but that's not much of a learning experience - and perhaps a bit hard to read
peirik__ has joined #ruby
<gr33n7007h> wnd: partition was my initial idea xD
<gr33n7007h> but i was doing something wrong, obviously, coz i couldn't get it to work lol
<wnd> group_by is unstable here becase if you don't control input array, you don't control the order of hash keys it returns
peirik__ has quit [Ping timeout: 256 seconds]
<wnd> [0, 1].group_by(&:zero?).values != [1, 0].group_by(&:zero?).values
Vonter has joined #ruby
jdmark has quit [Remote host closed the connection]
legit has quit [Ping timeout: 255 seconds]
legit has joined #ruby
<gr33n7007h> wnd: good observation! this is what happens when you don't unit-test xD
jdmark has joined #ruby
<gr33n7007h> and write spontaneous code :)
___nick___ has joined #ruby
___nick___ has quit [Client Quit]
___nick___ has joined #ruby
<FullMetalStacker> alright, back :-) I fixed my solution to not fiddle with the existing array, and it worked now :-)
<FullMetalStacker> arr2 = Array.new
<FullMetalStacker> return arr2
<FullMetalStacker> arr.each{ |n| arr2 << n if (n != 0) }.each{ |n| arr2 << 0 if n == 0 }
<FullMetalStacker> Now let me check your obvious much more elegant solutions, too :-)
<Rounin> Does it just take any 0 and put it at the end?
<Rounin> I mean, "just"... It's a perfectly fine thing to do
<FullMetalStacker> @Rounin: Yes, and it also solves the issue that I get floats 0.0 and it expects me to put them to the end as integers 0
<FullMetalStacker> @gr33n7007h arr.group_by(&:zero?).values.sum([]) this is so elegant :-) Thanks, I learned again new stuff!
<FullMetalStacker> how would I integrate that the floats are converted to integers with this solution?
<FullMetalStacker> ah got it
<FullMetalStacker> arr.group_by(&:zero?).values.sum([]).map(&:to_i)
<Rounin> 👍
<gr33n7007h> FullMetalStacker: that has gotchas! read what wnd wrote above.
<Rounin> You could go (arr.filter{|n| n != 0} + arr.filter{|n| n == 0}).map{|n| n.to_f}
<FullMetalStacker> Oh, I see. The group_by puts the group of zeros to the beginning if the first digit in the array is a 0 and if not, to the end. this is of course a problem
<Rounin> Or to only convert the zeros to floats: arr.filter{|n| n != 0} + arr.filter{|n| n == 0}.map{|n| n.to_f}
<Rounin> A little bit of code golf on the weekend is good fun
jidar has quit [Ping timeout: 246 seconds]
<Rounin> Oh, was it to integer... to_i then, of course
<Rounin> arr.filter{|n| n != 0} + arr.filter(&:zero?).map(&:to_i) Haha
<Rounin> Or, well, (arr.filter{|n| n != 0} + arr.filter(&:zero?)).map(&:to_i) to map all of them
<Rounin> Man, Ruby is fun
jidar has joined #ruby
<FullMetalStacker> @wnd what does the !v do?
<FullMetalStacker> ah, !v.zero? = not zero?
<gr33n7007h> FullMetalStacker: arr.sort{_1}.reverse.map(&:to_i)
<FullMetalStacker> this puts the zeros to the end, but also changes the others, no?
<FullMetalStacker> @Runin: Ruby is the most beautiful language of all, including human languages :-)
<gr33n7007h> FullMetalStacker: other integers barring zero are in the same order.
<gr33n7007h> i know the problem code doesn't have negative integers so this passes xD
<gr33n7007h> no it still passes.
<gr33n7007h> Rounin: it really is the greatest language ever created imho [fact] xD
<Rounin> I mean... We've taken a fair and unbiased poll
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #ruby
<gr33n7007h> Rounin: first rule of ruby channel: do not talk bad about ruby channel. second rule of ruby channel: do *NOT TALK* bad about ruby channel. third rule of ruby channel: if someone has bias towards ruby they are wrong! fourth rule of ruby channel: Ruby is Aweome!
<gr33n7007h> LOL
eddof13 has joined #ruby
<gr33n7007h> i quite like lua too, it's a great language tbh.
<Rounin> Hmmm... Is this according to the rules of the Ruby channel I wonder
<Rounin> I suppose it's also OK to like other things
peirik__ has joined #ruby
sagax has joined #ruby
<sagax> hi all
<sagax> rubygems has broken?
<gr33n7007h> Rounin: it's only a joke xD official rules are in the topic
<gr33n7007h> sagax: o/
<gr33n7007h> sagax: broken?
<sagax> >> This gem could not be found
<ruby[bot]> sagax: # => (https://carc.in/#/r/e3qq)
<sagax> yes, gem could not be found
<sagax> when i change any version, >> gem could not be found
<sagax> what it's is? i don't understand
peirik__ has quit [Ping timeout: 268 seconds]
<havenwood> FullMetalStacker: my first thought without looking at other solutions is:
<havenwood> zeros = Array.new(array.count(0), 0); array.delete(0); [*array, *zeros]
<havenwood> or maybe:
<havenwood> zeros = [0] * array.count(0); array.delete(0); [*array, *zeros]
<havenwood> FullMetalStacker: On second thought:
<havenwood> [*array.reject(&:zero?), *[0] * array.count(0)]
<havenwood> Or I think I like the slightly longer Array.new version: [*array.reject(&:zero?), *Array.new(array.count(0), 0)]
<havenwood> FullMetalStacker: My first destructive way seems to be about 3x as fast, FWIW.
gcd has quit [Ping timeout: 246 seconds]
peirik__ has joined #ruby
<havenwood> Here's a derivation of Rounin's solution: [*array.filter(&:nonzero?), *array.filter(&:zero?).map(&:floor)]
some14u has joined #ruby
<havenwood> That's about 8x slower than the destructive version. Might want to be going on readability rather than speed, but I don't know the judgement criteria. :)
<havenwood> Eww, I meant to write `&:to_i` not `&:floor` after considering for a sec and wrote the wrong one.
<havenwood> Or it a `filter_map` speeds it up a bit.
peirik__ has quit [Ping timeout: 260 seconds]
<havenwood> That gets it to about 6 times slower than destructive + counting.
* havenwood would rather race than golf :P
<havenwood> It's probably good to generally assume readability is prioritized above speed above golf.
* havenwood muses
cognemo has quit [Quit: ZNC 1.8.2 - https://znc.in]
cognemo has joined #ruby
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #ruby
finsternis has joined #ruby
<gr33n7007h> speed is always good though for certain circumstances.
eddof13 has quit [Quit: My MacBook Air has gone to sleep. ZZZzzz…]
markong has quit [Ping timeout: 256 seconds]
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #ruby
aindilis_ has quit [Remote host closed the connection]
aindilis has joined #ruby
infernix has quit [Ping timeout: 240 seconds]
alexherbo2 has quit [Remote host closed the connection]
<leftylink> |(a-[0]).concat([0]*a.count(0))
<leftylink> (a-[0]).concat([0]*a.count(0))
<leftylink> of course
alexherbo2 has joined #ruby
<leftylink> actually we should get rid of unnecessary parentheses
<leftylink> a-[0]+[0]*a.count(0)
<leftylink> perfect
alexherbo2 has quit [Remote host closed the connection]
alexherbo2 has joined #ruby
<Rounin> :D
<leftylink> I want to use fill but the args fill wants are unsuitable
boey89 has joined #ruby
infernix has joined #ruby
smp has quit [Ping timeout: 260 seconds]
smp has joined #ruby
desnudopenguino has quit [Quit: desnudopenguino]
desnudopenguino has joined #ruby
cartdrige has joined #ruby
___nick___ has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
___nick___ has joined #ruby
___nick___ has quit [Client Quit]
stirl has joined #ruby
___nick___ has joined #ruby
ur5us has joined #ruby
ur5us has quit [Remote host closed the connection]
___nick___ has quit [Ping timeout: 256 seconds]
boey89 has quit [Ping timeout: 260 seconds]
johnsonrabbit has joined #ruby
<FullMetalStacker> guys thank you all for your super feedback & input, I checked all solutions and learned again a ton of new smart things! THANK YOU ALL!
<FullMetalStacker> This channel is pure gold, so great!
<johnsonrabbit> Does anyone know why this code returns false? https://gist.github.com/wozza35/b7f1d56a7e5f851fe08b499ec013deb1
alexherbo2 has quit [Remote host closed the connection]
<johnsonrabbit> ah. Nevermind, it's comparing a String to an instance of MyClass
Guest335 has quit [Ping timeout: 252 seconds]
_ht has quit [Quit: _ht]
FullMetalStacker has quit [Read error: Connection reset by peer]
stirl_ has joined #ruby
stirl has quit [Read error: Connection reset by peer]
stirl_ is now known as stirl
stirl has quit [Client Quit]
stirl has joined #ruby
boey89 has joined #ruby
cartdrige has quit [Ping timeout: 256 seconds]
John_Ivan__ has quit [Read error: Connection reset by peer]
John_Ivan__ has joined #ruby
eddof13 has joined #ruby
gr33n7007h has quit [Quit: WeeChat 3.7.1]
dviola has quit [Ping timeout: 260 seconds]
dviola has joined #ruby
Aminda has quit [Ping timeout: 255 seconds]
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
boey89 has quit [Ping timeout: 256 seconds]
Aminda has joined #ruby
some14u has joined #ruby
Aminda has quit [Remote host closed the connection]
Aminda has joined #ruby
ur5us has joined #ruby
gr33n7007h has joined #ruby
boey89 has joined #ruby
eddof13 has quit [Quit: My MacBook Air has gone to sleep. ZZZzzz…]
ur5us has quit [Quit: Leaving]
ruby[bot] has quit [Remote host closed the connection]
ruby[bot] has joined #ruby
ur5us has joined #ruby
gcd has joined #ruby
peirik__ has joined #ruby
peirik__ has quit [Ping timeout: 268 seconds]
cryptkeeper has joined #ruby
markong has joined #ruby
peirik__ has joined #ruby
some14u has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
ur5us has quit [Ping timeout: 240 seconds]
some14u has joined #ruby
goepsilongo has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
goepsilongo has joined #ruby
stirl has left #ruby [#ruby]
legit has quit [Quit: WeeChat 3.5]
legit has joined #ruby