Simple problem

Hi all!

I am trying to substitude a single quote (') with
an escaped one (\')

I have tried so many things but I haven't managed it.

"this
​ ​
is a'test".gsub(/[']/,"
​\\'​
")

​Could anyone help ? ​

​Thanks a lot!
(Ruby version 2.3.0)​

···

--
--
Γιάννης Χαριτάκης
http://ych.gr

Hi,

Hi all!

I am trying to substitude a single quote (') with
an escaped one (\')

I have tried so many things but I haven't managed it.

The non-block form of #gsub takes a special format string as the
second argument which allows escapes like \1 (which refer to captured
groups in the regular expression of the first argument). Your error
this is in the second argument, not in the first one as you seem to
have concluded. You need to escape the backslash from both Ruby's
string parser and from #gsub's replacement parser. That is, to include a
backslash into the result, you need to quadruple your backslash (\\\\).
See this example:

irb(main):001:0> str="this
irb(main):002:0" ​ ​
irb(main):003:0" is a'test"
=> "this\n​ ​\nis a'test"
irb(main):004:0> str.gsub("'", "\\\\'")
=> "this\n​ ​\nis a\\'test"
irb(main):005:0> puts str.gsub("'", "\\\\'")
this

is a\'test
=> nil

Alternatively, you can use the block form of #gsub, which does not offer
the group capture referral feature directly (you could still use the
global variables $1, $2, etc. to get them):

irb(main):006:0> puts str.gsub("'"){ "\\'" }
this

is a\'test
=> nil

Greetings
Marvin

···

Am Wed, 4 May 2016 18:12:40 +0300 schrieb Ioannis Charitakis <ioannis@ych.me>:

--
Blog: http://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F

Thanks a lot for your fast answer.

I did tried what you suggested, but I got tricked by irb's response
which uses two backslashes :

irb(main):030:0> "thisis a'test".gsub("'","\\\\'")
=> "thisis a\\'test"

But everything is alright, if you use puts:
irb(main):031:0> puts "thisis
a'test".gsub("'","\\\\'")

thisis a\'test

Again, thanks for getting me out of this.

···

On Wed, May 4, 2016 at 6:23 PM, Marvin Gülker <m-guelker@phoenixmail.de> wrote:

Hi,

Am Wed, 4 May 2016 18:12:40 +0300 > schrieb Ioannis Charitakis <ioannis@ych.me>:

> Hi all!
>
> I am trying to substitude a single quote (') with
> an escaped one (\')
>
> I have tried so many things but I haven't managed it.

The non-block form of #gsub takes a special format string as the
second argument which allows escapes like \1 (which refer to captured
groups in the regular expression of the first argument). Your error
this is in the second argument, not in the first one as you seem to
have concluded. You need to escape the backslash from both Ruby's
string parser and from #gsub's replacement parser. That is, to include a
backslash into the result, you need to quadruple your backslash (\\\\).
See this example:

irb(main):001:0> str="this
irb(main):002:0" ​ ​
irb(main):003:0" is a'test"
=> "this\n​ ​\nis a'test"
irb(main):004:0> str.gsub("'", "\\\\'")
=> "this\n​ ​\nis a\\'test"
irb(main):005:0> puts str.gsub("'", "\\\\'")
this

is a\'test
=> nil

Alternatively, you can use the block form of #gsub, which does not offer
the group capture referral feature directly (you could still use the
global variables $1, $2, etc. to get them):

irb(main):006:0> puts str.gsub("'"){ "\\'" }
this

is a\'test
=> nil

Greetings
Marvin

--
Blog: http://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

You’re welcome. Though next time, please

a) Use a meaningful email subject (like "How to use gsub with a
   backslash?")
b) Do not top-post, but use proper quoting like I did in this post.

Greetings
Marvin

···

Am Wed, 4 May 2016 18:40:15 +0300 schrieb Χαριτάκης Ιωάννης <ioannis@ych.me>:

Thanks a lot for your fast answer. [...]
Again, thanks for getting me out of this.

--
Blog: http://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F

b) Do not top-post, but use proper quoting like I did in this post.

What is top-posting?

Thank you,

mendel

···

On Wed, May 4, 2016 at 2:55 PM, Marvin Gülker <m-guelker@phoenixmail.de> wrote:

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Responding on top and putting your quotes at the bottom
ref: top-post

Also, don't quote unnecessary parts of emails at the bottom
unless you're forwarding it to somebody new to the thread.
So leave the "Unsubscribe" footers out, too, to save
space and bandwidth.

It may not seem like a lot, but probably several thousand
people will see your message when posted to the list and
not being aware of the extra space and bandwidth use
causes performance problems for everyone.

Along the same lines, I also suggest disabling HTML email, too.
Your mail only had 0.3K of plain text (even with over-quoting),
but the HTML portion was 1.1K, so you ended up over 4 times the
space and bandwidth usage.

If everybody could be aware of how much bandwidth they're using,
(especially web designers), the Internet could be 3-4 times
faster! It's completely depressing that a modern webpage tries
to shove a million bytes of JS/styles/images just to display a
few thousand bytes of text.

Disabling HTML isn't a requirement here, but is of several
other lists, so you get better compatibility as well.

···

Mendel Schneerson <mmschneerson@gmail.com> wrote:

What is top-posting?

All suggestions accepted and appreciated.

···

--
--
Γιάννης Χαριτάκης
http://ych.gr

And how much bandwidth has been wasted by these verbose instructions on how to write emails to this list?

It seems that regularly threads turn to a discussion about how to use the mailing list, and I wonder just how useful that is. Back in the day when most people were reading their mail in a terminal, plain text and bottom-posting was more than just etiquette, but these days, I'm pretty sure most people are using email applications, which make navigating through email messages really easy.

Anyway, in terms of usability it is nice when everyone writes emails to the list using the conventions of the list, but obviously people don't see the benefit of doing this anymore and honestly they're no longer as necessary as they used to be. It might be time to just move on.

···

On 05.05.2016 03:11, Eric Wong wrote:

Mendel Schneerson <mmschneerson@gmail.com> wrote:

What is top-posting?

Responding on top and putting your quotes at the bottom
ref: top-post

Also, don't quote unnecessary parts of emails at the bottom
unless you're forwarding it to somebody new to the thread.
So leave the "Unsubscribe" footers out, too, to save
space and bandwidth.

It may not seem like a lot, but probably several thousand
people will see your message when posted to the list and
not being aware of the extra space and bandwidth use
causes performance problems for everyone.

Along the same lines, I also suggest disabling HTML email, too.
Your mail only had 0.3K of plain text (even with over-quoting),
but the HTML portion was 1.1K, so you ended up over 4 times the
space and bandwidth usage.

If everybody could be aware of how much bandwidth they're using,
(especially web designers), the Internet could be 3-4 times
faster! It's completely depressing that a modern webpage tries
to shove a million bytes of JS/styles/images just to display a
few thousand bytes of text.

Disabling HTML isn't a requirement here, but is of several
other lists, so you get better compatibility as well.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
Joe Gain

And how much bandwidth has been wasted by these verbose instructions
on how to write emails to this list?

I considered this, too; but I figured an email could convince
a few other people to quote less, it might be an overall win...

It seems that regularly threads turn to a discussion about how to
use the mailing list, and I wonder just how useful that is.

I think it's a useful reminder because this translates to
knowledge about performance that can help Ruby (or any
computing).

As a sometimes ruby-core dev; I get complaints about Ruby (and
other things :P) being slow and using too much memory; but much
of that blame is programmers become careless and complacent from
using powerful hardware that they forget users who cannot
upgrade all the time.

I know I wrote some of my slowest code right after I got more
RAM and faster CPUs :>

My hope is that if more people are aware of small everyday
things which affect performance; they can help improve Ruby
and the computing world.

Now, I still regularly use old, slower hardware. I notice
performance problems way more quickly that way.

<git old-timer tangent>
One of the most enlightening reads in the earlier days of
git was this discussion on tabs vs spaces in indentation
and how it affected performance of "git grep":

http://mid.gmane.org/20071018024553.GA5186@coredump.intra.peff.net
(tl;dr: tabs were 16% faster because they're more compact)

Size matters, locality matters. git's design awareness around
those things made git fast to start with; then it also benefitted
from continuous efforts to optimize small bits and pieces here
and there; adding up to huge wins.
</git old-timer tangent>

In contrast, Ruby wasn't designed for speed from the start;
and the internal data structures had poor locality.
However, mainline Ruby made improvements over the years:

* bytecode VM + inline caches improves memory locality
* generational GC reduces scanning of the heap
* more compact and better locality of internal tables
* reducing allocations and exceptions in common code

Sadly, much of the performance improvements made to Ruby gets
negated since Ruby applications get bigger and push more data
every year, too :<

I'm plenty aware git still has performance problems, too;
and performance problems are everywhere you look (including
the lack of compression of this message over SMTP)

···

Joe Gain <joe.gain@gmail.com> wrote:

On the other hand, more people read their mails on the phone,
and scrolling through pages of unedited quotation, like in
your post, might be easy but rather unnerving nevertheless.
Especially if you want to browse not only through one but
through dozens of posts.

And even on my netbook using a full-featured email application
I had to scroll down ;(

So I completely agree with Eric.

Regards,
Marcus

···

Am 05.05.2016 um 08:35 schrieb Joe Gain:

but these days, I'm pretty
sure most people are using email applications, which make navigating
through email messages really easy.

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

and I wonder just how useful that is.

In your email I had to scroll down quite a lot to find your actual
reply. And: Proper quoting makes it very easy to grasp what you’re
actually replying to. If you think this is out of date, then look at
all the web forums out there. One doesn’t quote an entire post in a web
forum. Instead, one quotes the parts they want to reply to, place the
answer below, and then make the next quote. In case you didn’t notice,
this is the very netiquette quoting style recommended for mailinglists.
It has been transferred to web forums for good reasons.

Back in the day when most people were reading their mail in a
terminal, plain text and bottom-posting was more than just etiquette,

"Modern" email clients actually tend to lack features that "old-school"
terminal clients have. Ask any mutt user of your choice.

Additionally, I do want people with terminal clients to be able to read
my mail, because these people tend to be fairly knowledgable and have
limited time frames. You can take Eric as an example -- I don’t know
which mail client he uses, but it’s definitely plaintext. And Eric has
authored several high quality Ruby libraries and contributed to MRI
itself. If you use HTML-only mail, you may likely not get an answer
from him.

but these days, I'm pretty sure most people are using email
applications, which make navigating through email messages really
easy.

It’s not about the application. If I need to search for your text then
I’m wasting time. These days I don’t entirely buy the bandwidth argument
anymore (except for really spammy HTML mails with lots of images), but
HTML mail is noisy still. Needless clutter that only serves to make
text more important than it is. If you need three different font sizes
to explain your argument, then you probably are in need of real
arguments.

Anyway, in terms of usability it is nice when everyone writes emails
to the list using the conventions of the list, but obviously people
don't see the benefit of doing this anymore

Counterargument: Because nobody explained it to them they do not even
know that proper quoting has advantages.

It might be time to just move on.

If I had written all of this email in one blob rather than quoting the
parts I respond to, you’d have to keep track of the entire conversation
in your head. If you read about 10 mailinglists like I do, that’s
fairly tedious. So: no, it is not time to move away from a useful
quoting style.

Greetings
Marvin

···

Am Thu, 5 May 2016 08:35:44 +0200 schrieb Joe Gain <joe.gain@gmail.com>:

--
Blog: http://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F

If you need three different font sizes
to explain your argument, then you probably are in need of real
arguments.

I really loved this one. Sorry, I had to say it.

Btw, the whole fuzz that started with my simple question,
makes me feel like the squirrel at the begining of every Ice Age movie.

End of interrupt.

···

--