Lewix has quit [Remote host closed the connection]
Johz has joined #ruby-lang
ledestin has quit [Quit: ledestin]
agarie has joined #ruby-lang
robmiller has quit [Quit: robmiller]
agarie has quit [Ping timeout: 245 seconds]
agarie has joined #ruby-lang
priodev has quit [Ping timeout: 272 seconds]
vlad_starkov has quit [Remote host closed the connection]
diegoviola has joined #ruby-lang
clamstar has joined #ruby-lang
symm- has joined #ruby-lang
clamstar has quit [Excess Flood]
nifoc has joined #ruby-lang
Mon_Ouie has joined #ruby-lang
Mon_Ouie has joined #ruby-lang
nifoc has quit [Client Quit]
nifoc- has joined #ruby-lang
anulman has joined #ruby-lang
clamstar has joined #ruby-lang
vlad_starkov has joined #ruby-lang
vlad_starkov has quit [Remote host closed the connection]
vlad_starkov has joined #ruby-lang
anulman1 has joined #ruby-lang
clamstar is now known as rx
anulman has quit [Ping timeout: 240 seconds]
<hfp>
Good morning, I am running into a stupid issue: I would like to read more about the shovel operator vs the plus equal operator in the doc. But I don't know what to search for. `<<` and `+=` don't return what I want.
clamstar has joined #ruby-lang
clamstar has quit [Client Quit]
<apeiros>
hfp: += is not a method, it's syntax sugar
<apeiros>
<< is a method, and what it does depends on the receiver
<apeiros>
you search for methods using e.g. `ri Array#<<` in your shell
nathanstitt has joined #ruby-lang
<apeiros>
or by going on ruby-doc.org or rdoc.info
anulman1 has quit [Ping timeout: 245 seconds]
<apeiros>
re +=: a += b is syntax sugar for `a = a + b`, and + being a method, it's the same game again as with << - what it does depends on the receiver.
<apeiros>
hth
<hfp>
Ok thanks. But say I had no idea what `+=` does, it must be explained somewhere in the doc, right?
<apeiros>
define "the doc"
<hfp>
ruby-doc.com
<apeiros>
it might be somewhere in `man ruby`, no idea.
<apeiros>
ruby-doc.com is API docs. it does not document syntax.
kalehv has quit [Remote host closed the connection]
<r0bgleeson>
hfp: hi points at the same object as original_string, & you call a method which mutates the object.
<hfp>
r0bgleeson: Ok and why if I use `+=` instead of `<<` then the original_string is unchanged?
<hfp>
(besides, how do you know what mutates an object and what doesn't?)
micalexa_ has quit []
<r0bgleeson>
hfp: += creates a new string through String#+, something like: "foo" + "baz". you can sometimes tell through a bang(!) at the end of the method name.
<r0bgleeson>
it's not consistently used like that
<r0bgleeson>
if you have a string you don't want to allow mutation on, you can say "string".freeze.
<certainty>
hfp: x += y is syntactic sugar for x = x + y .. so it invokes #+ on x which does not mutate the receiver
kwando has joined #ruby-lang
<hfp>
Where can i find definitions for all these words like 'receiver' and 'mutates an object' etc? I feel like it's holding me back not to have a clear definition of these concepts.
<r0bgleeson>
'receiver' refers to the object a method is being called on when we're talking here, 'mutates an object' means you have changed its state(perhaps an instance variable), so that it is no longer the same object.
<r0bgleeson>
a string is a core class and doesn't have instance variables to mutate, so in that case it means to change the same object.
phansch has quit [Remote host closed the connection]
<certainty>
many languages think that it's good to have immutable strings. ruby does not, for reasons i don't know
<hfp>
I see, and this is visible in the doc because for #+ it says `→ new_str` whereas for #<< it says `→ str`, correct?
<hfp>
So whenever there is a `new__` returned then it mutates, otherwise it doesn't?
<hfp>
new_*
<r0bgleeson>
that may be a guideline to look for, yeah, generally it's probably not that consistent, in this case that's what it means.
<certainty>
new_ means it doesn't. You get a fresh copy
<hfp>
Yes I got mixed up, that what I meant
ssb123 has joined #ruby-lang
<hfp>
Thanks
<r0bgleeson>
you're welcome!
AKASkip has quit [Read error: Operation timed out]
elliotec has joined #ruby-lang
CJD14 has quit [Ping timeout: 245 seconds]
diegoviola has quit [Quit: WeeChat 0.4.2]
elia has quit [Quit: Computer has gone to sleep.]
ssb123 has quit [Ping timeout: 240 seconds]
diegoviola has joined #ruby-lang
yfeldblum has joined #ruby-lang
thmzlt has joined #ruby-lang
vlad_starkov has quit [Ping timeout: 245 seconds]
ecnalyr has quit [Quit: Macbook has gone to sleep. . .]
elliotec has quit [Ping timeout: 272 seconds]
thmzlt_ has quit [Ping timeout: 245 seconds]
yfeldblum has quit [Ping timeout: 245 seconds]
bzalasky has joined #ruby-lang
guardianx has quit [Remote host closed the connection]
bzalasky has quit [Remote host closed the connection]
bzalasky has joined #ruby-lang
nszceta has joined #ruby-lang
KM has joined #ruby-lang
vlad_starkov has joined #ruby-lang
wallerdev has joined #ruby-lang
shevy has joined #ruby-lang
sapphiriq has joined #ruby-lang
vlad_starkov has quit [Remote host closed the connection]
elia has joined #ruby-lang
makoto__ has quit [Remote host closed the connection]
sapphiriq has left #ruby-lang [#ruby-lang]
sapphiriq has joined #ruby-lang
sapphiriq has left #ruby-lang [#ruby-lang]
DouweM has joined #ruby-lang
heavyhorse has joined #ruby-lang
jsrn has quit [Ping timeout: 252 seconds]
rx has quit [Killed (pratchett.freenode.net (Nickname regained by services))]
DEac- has quit [Quit: leaving]
dik_dak has joined #ruby-lang
clamstar has joined #ruby-lang
postmodern has quit [Quit: Leaving]
nszceta has quit [Ping timeout: 252 seconds]
diegoviola has quit [Quit: WeeChat 0.4.2]
imjacobclark has quit [Remote host closed the connection]
imjacobclark has joined #ruby-lang
Cakey has joined #ruby-lang
makoto_ has joined #ruby-lang
yfeldblum has joined #ruby-lang
diegoviola has joined #ruby-lang
Lewix has joined #ruby-lang
makoto_ has quit [Ping timeout: 272 seconds]
thmzlt has quit [Ping timeout: 272 seconds]
DEac- has joined #ruby-lang
jamto11 has quit [Remote host closed the connection]
mistym has joined #ruby-lang
mistym has quit [Ping timeout: 245 seconds]
kurko_ has joined #ruby-lang
centrx has joined #ruby-lang
diegovio1 has joined #ruby-lang
diegoviola is now known as Guest34373
diegovio1 is now known as diegoviola
Guest34373 has quit [Ping timeout: 252 seconds]
jonr22 has joined #ruby-lang
Barrin6 has joined #ruby-lang
makoto_ has joined #ruby-lang
makoto_ has quit [Ping timeout: 265 seconds]
robmiller has joined #ruby-lang
makoto_ has joined #ruby-lang
chouhoulis has quit [Remote host closed the connection]
brooks has joined #ruby-lang
sepp2k has quit [Read error: Connection reset by peer]
dik_dak has quit [Remote host closed the connection]
kurko_ has quit [Quit: Computer has gone to sleep.]
enebo has quit [Quit: enebo]
brooks has quit [Quit: brooks]
MindfulMonk has joined #ruby-lang
shinnya has quit [Ping timeout: 248 seconds]
arBmind has joined #ruby-lang
Cakey has quit [Ping timeout: 272 seconds]
vlad_starkov has joined #ruby-lang
vlad_starkov has quit [Read error: Connection reset by peer]
diegoviola has quit [Ping timeout: 252 seconds]
jamto11 has joined #ruby-lang
ssb123 has joined #ruby-lang
imjacobclark has quit [Remote host closed the connection]
imjacobclark has joined #ruby-lang
imjacobclark has quit [Read error: Connection reset by peer]
fonz has joined #ruby-lang
elliotec has joined #ruby-lang
imjacobclark has joined #ruby-lang
ssb123 has quit [Ping timeout: 240 seconds]
elliotec has quit [Ping timeout: 240 seconds]
imjacobclark has quit [Ping timeout: 252 seconds]
jamto11 has quit []
beawesomeinstead has joined #ruby-lang
vlad_starkov has joined #ruby-lang
elia has quit [Quit: Computer has gone to sleep.]
kalehv has joined #ruby-lang
amerine has quit [Quit: Computer has gone to sleep.]
lsegal has joined #ruby-lang
mistym has joined #ruby-lang
<Lewix>
Hi guys
rahul_j has joined #ruby-lang
<centrx>
Ahoy
shevy has left #ruby-lang ["I'll be back ... maybe"]
yfeldblum has quit [Read error: Connection reset by peer]
vlad_starkov has quit [Remote host closed the connection]
diegoviola has joined #ruby-lang
vlad_starkov has joined #ruby-lang
arBmind has quit [Ping timeout: 272 seconds]
Barrin6 has quit [Quit: Leaving]
relix has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
mykoweb has quit [Remote host closed the connection]
yfeldblum has joined #ruby-lang
mykoweb has joined #ruby-lang
mykoweb has quit [Remote host closed the connection]
fragamus has quit [Quit: Computer has gone to sleep.]
AKASkip has joined #ruby-lang
houhoulis has joined #ruby-lang
ledestin has joined #ruby-lang
thmzlt has joined #ruby-lang
benanne has quit [Ping timeout: 272 seconds]
<hfp>
Hi Lewix
makoto_ has quit [Remote host closed the connection]
<hfp>
So back to `+=` vs `<<`, programmers favor `<<` because it doesn't create a new object and saves ressources? Or is it for another reason that I missed?
<apeiros>
programmers favor what's best for the situation
<apeiros>
that's why both exist, after all
<apeiros>
otherwise we could just do away completely with one of them
<rue_>
That’s kind of a bold claim
<rue_>
The latter even more so
<waxjar>
if you didn't create the string yourself, it's probably better to use +=, if you did, you can use << and save some resources if you want
<centrx>
hfp, array += [1,2,3]
<centrx>
hfp, array << 1
<apeiros>
rue_: sorry, I don't bite :-p
<rue_>
There’s so much old crap lying around because it seemed like a good idea, over-engineering, BDUF, backwards compatibility, familiarity from other places
<centrx>
array << 1; array << 2; array << 3
yfeldblum has quit [Read error: Connection reset by peer]
<rue_>
Not in Ruby specifically, but it’s not exempt either
yfeldblum has joined #ruby-lang
relix has joined #ruby-lang
rahul_j has quit [Quit: rahul_j]
havenwood has quit []
<hfp>
I'm still confused. `+=` vs `<<` only makes sense if you first did something like `foo = bar; bar << 1; p foo` then?
<apeiros>
hfp: + and << are two different methods. that's all.
<apeiros>
they do different things.
<apeiros>
again, a += b is really a = a + b
<rue_>
#+ produces a new array by concatenating two existing ones. #<< inserts an element into an array
<hfp>
apeiros: I don't know it feels like I'm not getting the questions from the koans. They specifically ask this: `Ruby programmers tend to favor the shovel operator (<<) over the plus equals operator (+=) when building up strings. Why?`
<rue_>
“Because they assume mutation is faster”
<hfp>
faster in execution time?
<apeiros>
"because they assume less objects being created is better"
<apeiros>
I find that a rather bad question in Koans
benanne has joined #ruby-lang
<hfp>
Right but += doesn't create a new object
<apeiros>
yes it does
<waxjar>
it does
<apeiros>
with String#+=
jamto11 has joined #ruby-lang
<apeiros>
well, with String#+
<apeiros>
c = a + b; c is neither .equal?(a) nor .equal?(b)
<apeiros>
c = a << b; c is .equal?(a)
havenwood has joined #ruby-lang
<waxjar>
like apeiros said, x += y is syntax sugar for x = x + y
<hfp>
Let's say we have a = 1; a += 2; then no new object is created, there is still only `a`, right?
<apeiros>
hfp: the question is specifically asking for strings
<apeiros>
`when building up strings`
naquad has quit [Ping timeout: 252 seconds]
<apeiros>
as I repeatedly said, what a method does depends on the receiver.
<apeiros>
String#+ is not the same as Integer#+
<apeiros>
neither is String#<< the same as Integer#<< (those differ even more)
<waxjar>
hpf: a now points to a different object
jonr22 has quit [Remote host closed the connection]
dbussink has quit [Ping timeout: 265 seconds]
<apeiros>
hfp: and a = 1; a += 2 not creating a new object is only because the object `3` already exists.
<apeiros>
add a couple of zeros (forcing you into BigNum), and that no longer holds true.
dbussink has joined #ruby-lang
<apeiros>
hfp: also, don't confuse objects and variables
naquad has joined #ruby-lang
<apeiros>
`a` is not an object, it's a variable, which references an object
<hfp>
Ok so I see with this https://eval.in/96962 that one case creates a new object. However, what happens to the "old" `a`? Can it still be accessed?
<apeiros>
a = "foo"; a += "bar" # one variable, three involved objects - "foo", "bar" and the resulting "foobar"
<r0bgleeson>
apeiros: pure functional languages? :p
<apeiros>
hfp: no, you can't access the object which a referenced before anymore
saarinen has joined #ruby-lang
relix has quit [Quit: My MacBook Pro has gone to sleep. ZZZzzz…]
<apeiros>
hfp: with `a = ` you tell a to which object it references
<apeiros>
any old reference is lost
<apeiros>
you can of course tell another variable to reference to that object before reassigning `a`
<apeiros>
a = "foo"; b = a; a += "bar"; b # => "foo"
<hfp>
And why is it that if I do `a = "foo"; a += "bar"; a = "foo"` I have three diferent object_id? I would expect the first and last `a` to have the same object_id?
<apeiros>
hfp: why do you expect that?
<apeiros>
hfp: because those strings have the same content?
<hfp>
apeiros: Because I'm confused :)
<apeiros>
hfp: the expression `"foo"` creates a new object already
<waxjar>
hfp they're not the same object, because they can be changed (with << for example)
<apeiros>
you don't even have to assign it anywhere
<apeiros>
this is true for many literals:
<waxjar>
if i use the string "foo" in one part of my program (let's call it part a), and in another part i do "foo" << "bar", the "foo" in part is now too "foobar" :O
<apeiros>
and those objects all share one property: they are immutable. hence it has no bad side effect that you only ever get one instance of them.
sepp2k has joined #ruby-lang
mistym_ has joined #ruby-lang
<hfp>
I see, that's why if something comes back often it's better to use a symbol, it uses only one object? Whereas using a string creates as many objects as many times the string is assigned?
<waxjar>
exactly
mistym has quit [Ping timeout: 245 seconds]
<waxjar>
symbols are also not collected by the garbage collector so they stay in memory once you create them
<apeiros>
which makes it a bad idea to create symbols from arbitrary (non-whitelisted) userinput
<rue_>
GC is the wrong reason to use (or not use) symbols
<rue_>
You use a symbol when you want to have a descriptive value meaningful to the programmer, that you don’t need to modify
<waxjar>
a string would work just as well under that description.
<rue_>
That’s what after the comma is about
<rue_>
Like cd_drive_type = :bluray (rather than 'bluray', or worse, 1 or some other opaque, unexplained identifier)
<rue_>
But yeah, you could use a string if you wanted.
<apeiros>
Symbol is almost exclusively a performance optimization
<rue_>
Maybe an additional restriction would be that you wouldn’t print it either
<r0bgleeson>
hfp: a symbol is similar-ish to an immutable string in ruby
<apeiros>
it's very very rare that you couldn't use a string in its place
<rue_>
apeiros: In current usage? Yes. But I think it can be useful in signifying that something is an /internal/ label
<apeiros>
hence, when in doubt, use a string.
<rue_>
But I’d agree with ^
<apeiros>
rue_: I usually phrase it as: "if it's an identifier, use a symbol"
<chris2>
i recently rewrote some goto-heavy code with a case-loop of integers. should have used symbols
<apeiros>
as that's the main reason symbols exist. to identify ruby internal stuff.
<apeiros>
chris2: you compiled ruby with SUPPORT_JOKE?
<chris2>
no, i ported algol 60 code :P
<whitequark>
chris2: integers are as efficient as symbols in this regard