last day (15 days later) » 

9:31 PM
-1
A: Ruby is returning actual newlines instead of \n

ndnYou are still reading "Foo\n\nBar". However, puts interprets the special characters. You can use String#inspect: puts content.inspect # => "Foo\n\nBar"

 
This is not safe. It will put it in quotes. It will also do the wrong thing if the object is not a String object, it will show the class. The format of Object.inspect should not be relied upon to be anything but human readable. inspect's behavior can be overridden.
 
ndn
@muistooshort, you can look at it that way too.
 
@sawa For this exact piece of code it is probably safe for the current version of Ruby. As a general purpose technique for escaping special characters it is not. You're handing the OP a gun without explaining the safety rules.
 
ndn
@Schwern, so your argument for downvoting is that if you pass it something, which you shouldn't pass, you will get a strange result and that if someone arbitrary monkey patches the core string class it might not work?
 
@ndn My argument is the OP is going to use this technique for escaping special characters on a string-like that doesn't come from a file and not understand why it doesn't work.
@sawa That's A) very lazy to limit the answer to one that only works for the exact details of the question, B) might break in a future version and C) should be made extremely clear to the OP who is obviously not experienced to know that.
 
ndn
9:31 PM
@Schwern, I still don't get your point. Can you give me an example where puts any_string.inspect wouldn't do exactly what OP wants?
@sawa, could you elaborate? What do you mean by fake the result?
 
@ndn ruby -e 'require "uri"; string = URI("http://example.com"); puts string; puts string.inspect'
 
ndn
@Schwern, um... you do realize that string here is not a String?
 
@ndn Yes, it's a thing that acts like a string and it could be returned by a method that returns a "string". That's the whole point of duck typing. Why tempt fate with inspect, and get the wrong format because of the quotes, when thing.to_s.gsub works without caveats?
 
ndn
@Schwern, by that logic I shouldn't use any of the Array's method in my answers because someone might want to use them for something that acts like an array, but implements some methods to do completely different things. And no, URI::HTTP is not intended to be a sort of string, it can just be visualized as a string...
 
@ndn No, those overrides would be a violation of the Array interface and considered bugs. You're violating Object.inspect's interface by using it outside its defined behavior. It only guarantees a "human readable representation". The docs encourage that "user defined classes should override this method to provide a better representation of obj" String.inspect overrides the default behavior, it is normally "object’s class name, an encoding of the object id, and a list of the instance variables and their values".
 
ndn
9:31 PM
@Schwern, in what way is that an argument? Lets say you have a hierarchy with Animal, Dog < Animal, Cat < Animal. And Animals have make_sound method, which in Dog is implemented as "bark". A person is asking me right now how do I make a dog bark?. I answer with dog.make_sound. And you say - no!, that is a terrible idea, because the default implementation of make_sound is "grrrr" and a Cat might implement an entirely different thing to! WHAT?
 
I think part of the disagreement here is I've interpreted the question as "how do I escape newlines" but it might also be "how do I emulate the behavior of irb". If it's the latter, this answer is correct.
There is a difference between the default implementation and the documented implementation.
If the documentation says "Animal.make_sound returns the sound the animal would plausibly make" then either "grr" or "bark" is fine.
And if it said that, assuming that Dog.make_sound will always say grrr because it says grrr right now is wrong.
 
ndn
so your argument is that because it's not explicitly stated, String#inspect might return something completely different in the future?
no
the assumption here is that Dog.make_sound makes "bark"
 
That's a bad assumption.
The docs for String.inspect are pretty clear about its format, so it's probably stable. ruby-doc.org/core-2.2.3/String.html#method-i-inspect
The problem is that if you have a String subclass, or a String-like thing, you're hosed.
 
ndn
then what is your argument?
 
Because Object.inspect makes no guarantees about what inspect should do except that it's a human readable format in the current encoding.
 
ndn
9:35 PM
If you make a subclass of string and don't implement backwards compatible methods it's your fault
doesn't*
 
Fair point, but Ruby is heavily into Duck typing.
I don't know if you caught my last comment, I think we've interpreted this question differntly.
I've interpreted it as "how do I escape special characters", but it could also be read as "how do I emulate the output of irb".
 
ndn
And a duck type would consist of all the methods you indent to use. By your logic, what if my subclass of string implements inspect the way string does, but to_s returns 55? Then your version doesn't work
 
Lots of string-like things (and yes, I'm counting URI) don't implement inspect as String.inspect does.
 
ndn
but they are not intended to be strings, they just have string representation. everything ever has string representation.
 
You have a good point.
 
ndn
9:39 PM
so are we in agreement? xd
 
About that part, yes.
I still think it's a hideous method to escape characters, but I think we're reading the question differently.
 
ndn
"Returns a printable version of str, surrounded by quote marks, with special characters escaped." -> the documentation says it... Also the question wasn't asking "how do I escape this", it was asking "how do I see this for what it truely is/what it is inside", inspect is just 100% perfect
 
I put weight on the "I need that variable as a single line for what comes next in my script" part. We'll see what the OP says.
The answers cover all the bases.
 
ndn
could you remove the downvote?
 
Can't until it's edited. I'm waiting for the edit to be approved.
 
ndn
9:52 PM
I approved it
 
Done.
 
ndn
thanks
btw, it's almost 01:00 here, I'll go to sleep soon and check again tomorrow
 
No worries. Thanks for the discussion!
 

last day (15 days later) »