lunarkitty has quit [Remote host closed the connection]
lunarkitty has joined #ruby
dhruvasagar has joined #ruby
waesum has joined #ruby
konsolebox has joined #ruby
<waesum>
I've been looking into Ruby the past few days, and have been messing around with some code I've found online to familiarize myself with the syntax. When modifying a FizzBuzz i found on SE, I'm not quite sure I understand the behaviour of the parenthesis in this code: https://pastebin.com/saErVjrU Can anyone explain the significance of the parenthesis around the conditional assignment and why the second
<ruby[bot]>
waesum: we in #ruby do not like pastebin.com, it loads slowly for most, has ads which are distracting and has terrible formatting. Please use https://gist.github.com or https://dpaste.org/
<leftylink>
and we can see that '' << 'Buzz' is just 'Buzz', which owuld make that line `out ||= 'Buzz' if (i % 5).zero?` which we can see is obviously not what we want
<leftylink>
we can see that `out ||= 'Fizz' if (i % 3).zero?` on the other hand is perfectly fine
dhruvasagar has joined #ruby
hwpplayer1 has joined #ruby
hiddenman has quit [Ping timeout: 252 seconds]
<leftylink>
honestly whoever wrote that code spent extra effort to make the first line look like the second line, which makes the first line have unnecessary parts
<leftylink>
I think that is harmful for understanding.
<leftylink>
the first line should just be `out = 'Fizz' if (i % 3).zero?` . that's it.
<leftylink>
none of that other shit
<leftylink>
I feel strongly about this
<waesum>
leftylink: yes, i think they were just doing that for symmetry
dhruvasagar has quit [Ping timeout: 260 seconds]
dhruvasagar has joined #ruby
<waesum>
leftylink: i'm just not sure i understand the significance of the parenthesis in this case. the way i read it, out ||= '' << 'Buzz' if (i % 5).zero? would compute the same
dhruvasagar has quit [Ping timeout: 244 seconds]
<leftylink>
||= lower precedence than <<
<leftylink>
so without the parenthesis, << happen first, making `'' << 'Buzz'` just `'Buzz'`
<waesum>
in other words, the << has no practical meaning if the conditional assignment doesnt take precedence?
dhruvasagar has joined #ruby
<leftylink>
I... am hesitant about that statement because regardless of whether it's true I don't think it explains the behaviour
<leftylink>
I think looking at the intended behaviour of the << is important
<waesum>
which is to append a non-falsy value to the variable?
<waesum>
coming from a java background ruby has a lot of magic to me
hwpplayer1 has quit [Quit: bbl]
dhruvasagar has quit [Ping timeout: 246 seconds]
<leftylink>
I don't think we should bring falsiness into the answer to this question because << is only ever being used with truthy args and receivers, though certainly it does have to append, otherwise we can't have gotten from Fizz to FizzBuzz, so that part's right
<waesum>
leftylink: i think i got it. the default precedence interprets the line basically as `out ||= ('' << 'Buzz' if (i % 5).zero?)` if i don't specify that it isn't what i want
<waesum>
without the << it wouldn't work because + would create a new string which doesn't get kept anywhere
<leftylink>
when I was looking at the code I also wanted to see also why the ||= is necessary. why isn't that line just `out << 'Buzz' if (i % 5).zero?` ? because that would work for the 15 case... so what case doesn't it work for? I asked myself that question so that I could be the remaining parts of the code were actually necessary to be there
<leftylink>
since I went to the trouble of criticising the first line for having extra unnecessary shit, it behooves me to make sure I haven't left out the second line too
<waesum>
won't that cause it to try to call << on a nil object if the fizz string wasnt already there?
dhruvasagar has joined #ruby
<leftylink>
indeed and this tells us why ||= is needed
<leftylink>
huh, irrelevant aside, so I suppose we could make it work by defining << on NilClass, but not something that actually seems reasonable to do in a real codebase, so I will discard the idea
<waesum>
i was actually thinking how i would do just that out of curiosity, but to make it work i would have to make the object "replace itself", which im not sure how to do :)
dhruvasagar has quit [Ping timeout: 248 seconds]
<waesum>
since `class NilClass def <<(s) return s end end` won't cut it
<waesum>
at least it wont crash though
<leftylink>
oh true. hmmmmmm
<leftylink>
Ruby doesn't have become like Smalltalk does, so maybe it's not possible
konsolebox has joined #ruby
<waesum>
either way you cleared it up to me, so thanks!
grenierm has quit [Ping timeout: 256 seconds]
<leftylink>
I'm glad I was able to help. I hope I don't lose the ability to explain things one day. I will work harder to continue to be able to do so
dhruvasagar has joined #ruby
dhruvasagar has quit [Ping timeout: 265 seconds]
dhruvasagar has joined #ruby
hiddenman has joined #ruby
Cork has quit [Quit: .]
Cork has joined #ruby
konsolebox has quit [Ping timeout: 252 seconds]
___nick___ has joined #ruby
kiwi_36 has joined #ruby
dhruvasagar has quit [Ping timeout: 272 seconds]
dhruvasagar has joined #ruby
<isene>
Possibly a classic, but can't find an elegant solution; I need to split a long string of words into substrings no longer than X in length (I'm creating an alternative to ncurses in pure Ruby and need a way to fit text into paragraphs in boxes).
konsolebox has joined #ruby
dhruvasagar has quit [Ping timeout: 248 seconds]
dhruvasagar has joined #ruby
konsolebox has quit [Ping timeout: 240 seconds]
dhruvasagar has quit [Ping timeout: 272 seconds]
dhruvasagar has joined #ruby
hiddenman has quit [Ping timeout: 260 seconds]
dhruvasagar has quit [Ping timeout: 255 seconds]
dhruvasagar has joined #ruby
Versality has joined #ruby
<adam12>
isene: Maybe other ways but you could scan and then capture the missing pieces.
<constxd>
however if you do insist on it, you likely don't want such a simplistic algorithm for laying out text
<isene>
Because ruby curses interface sucks elephants through garden hoses
<constxd>
maybe have a look at nroff(1) or fmt(1)
<isene>
I'm looking for innsparing for the text handling
<constxd>
ncurses is garbage but it does one very important thing
<isene>
Autocorrect (norwegian)
<isene>
... inspiration
<constxd>
and that is to track the contents of the screen and diff it against what you want to put on the screen so you can get to your desired result with the minimum amount of i/o
<constxd>
you won't be able to do that veryy efficiently in pure ruby
<constxd>
plus ncurses takes care of reading terminfo entries and making sure to be compatible with lots of different terminals. if you care about that
konsolebox has joined #ruby
dhruvasagar has quit [Ping timeout: 265 seconds]
dhruvasagar has joined #ruby
dhruvasagar has quit [Remote host closed the connection]
konsolebox has quit [Ping timeout: 265 seconds]
Guest45 has joined #ruby
<isene>
constxd: All that can be done in ruby alone, simply by manipulating ANSI escape sequences, which I'm about to
Guest45 has quit [Quit: Client closed]
___nick___ has quit [Ping timeout: 252 seconds]
___nick___ has joined #ruby
<constxd>
isene: yes of course it can be done in ruby. but what i'm saying is it will be rather slow
<constxd>
and non-portable if you just hardcode escape sequences
Versality has quit [Remote host closed the connection]
Versality has joined #ruby
Versality has quit [Remote host closed the connection]
lunarkitty has quit [Remote host closed the connection]
ruby[bot] has quit [Remote host closed the connection]
ruby[bot] has joined #ruby
lunarkitty has joined #ruby
vvn has joined #ruby
<vvn>
hi there -- let's say I have two independent module, one module Foo which parses some data from JSON and one module Bar which produces data in YAML format, what is the clean Ruby way to implement code to export Foo data to Bar data?
<vvn>
a third module FooBar which requires both modules?
<vvn>
or mixin Bar into Foo?
<constxd>
not enough info man
<constxd>
if u can do it in such a way that Foo and Bar never need to know about each other then u should do so
<vvn>
yes that's my concern, how to properly write code that use two independent, in the typical scenario of having some data that you export in another format