[#408634] How do I make lots of classes aware of each other? — "Andrew S." <lists@...>

I'm apparently missing something fundamental in my knowledge of classes

10 messages 2013/07/02

[#408712] Ruby web service with REST support — "Shubhada S." <lists@...>

Hi All,

17 messages 2013/07/05

[#408812] create variables depending on counter — stefan heinrich <lists@...>

Hi community,

21 messages 2013/07/09

[#408854] execute commands within SMTP email code: send content in variables and not actual variables — dJD col <lists@...>

I am trying to send an email using the code below. I am able to send the

9 messages 2013/07/10

[#409031] tap { break } idiom deserves its own Kernel method? — Andy Lowry <lists@...>

I use this idiom from time to time:

13 messages 2013/07/22

[#409072] Link To Masses Of External Data In Openoffice? — "Austin J." <lists@...>

This is what I want to do.

19 messages 2013/07/23
[#409102] Re: Link To Masses Of External Data In Openoffice? — Tamara Temple <tamouse.lists@...> 2013/07/24

[#409103] Re: Link To Masses Of External Data In Openoffice? — "Austin J." <lists@...> 2013/07/25

tamouse m. wrote in post #1116598:

[#409122] Re: Link To Masses Of External Data In Openoffice? — Tamara Temple <tamouse.lists@...> 2013/07/26

[#409142] Re: Link To Masses Of External Data In Openoffice? — "Austin J." <lists@...> 2013/07/26

tamouse m. wrote in post #1116750:

[#409073] class <=> module — Bráulio Bhavamitra <lists@...>

Hello all,

17 messages 2013/07/23

[#409104] Ruby newbie question on Methods (NoMethoderror) — "Crispian A." <lists@...>

I have recently started learning ruby and so I am writing a small little

10 messages 2013/07/25

[#409170] Working through Ch.10 for learning to program 2.0 (Chris Pine) — JD JD <lists@...>

So, I have been working through this book, and have been doing ok up

33 messages 2013/07/28
[#409195] Re: Working through Ch.10 for learning to program 2.0 (Chris Pine) — Harry Kakueki <list.push@...> 2013/07/29

I tried this and came up with a one-liner that seems to do it. It sorts the

[#409258] WATIR - ScriptError popup on IE - Unable to get rid of! — Graeme Halls <lists@...>

I am new to Ruby & Watir, and I am having a nightmare with IE and Script

11 messages 2013/07/31

Re: replace words using gsub

From: Robert Klemme <shortcutter@...>
Date: 2013-07-26 06:34:18 UTC
List: ruby-talk #409123
On Fri, Jul 26, 2013 at 7:07 AM, Brandon Weaver <keystonelemur@gmail.com>wrote:

> The k is an iterator. Let's use another example here to demonstrate:
>

k is not an iterator but takes on every iterated value. The iterator is the
piece of code which is driving this behind the scenes.

This article gives a good overview:
https://www.informit.com/articles/article.aspx?p=1398604


> array = [1,2,3]
> array.each{ |i| puts i * i }
>
> The iterator is put in i (it could be foobar for all Ruby cares, as long
> as the name is consistant.) The first time the block of code is executed, i
> is set to 1 and prints 1, then is set to 2 and prints 4, then is set to 3
> and prints 9. What you are doing is passing each element to an anonymous
> function called a block, which comes from some functional type programming
> with Lambda.
>

Right.


> Now on to the other part, I assume you know what a hash is already, if not
> check the docs for Ruby (very very useful.)
>
> Let's say we have string:
> string = "you up in there"
>
> ...and subs as you specified it. We need to iterate through the subs hash,
> so we do as so:
> subs.each_pair{ |k, v| string.gsub!(k,v) }
>

Why?  The original code did not do this.


> ...but why are there TWO iterators there? Who said there was a limit? You
> could probably (impractically) jam a number of them in there. In this case
> we use each_pair, which returns a key and a value (hence k, v) that we use
> for substitution. You will notice that I use the gsub! method instead of
> gsub, as gsub returns a new string instead of modifying the one in place.
> Do note that in functional programming it is frowned upon, which is why
> there's a ! at the end of the method indicating it modifies the object.
> There are ways to prevent side effects which may very well lead to cleaner
> code, and it will be worthwhile to look into this.
>

Actually Matz's reasoning about the exclamation mark is different: methods
which are potentially "dangerous" get an exclamation mark. For example,
Kernel#exit! does not have the sort of side effects that String#gsub has
but gets an exclamation mark nevertheless.


> Really, if I were you I would get intimately familiar with the Ruby docs,
> especially Enumerable. Learning how to properly learn blocks, procs, and
> lambdas is what tends to be a graduating point into more advanced code and
> as such would be good to look into as well. Try out
> http://www.rubymonk.com as well later, it should help.
>

That is excellent advice.

To the original code:

> subs = {
>     "you" => "he",
>     "up" => "down",
>     "in" => "out"}
>
> subs.default_proc = proc {|k| k}
> body.gsub(/(?=\b).+(?=\b)/, subs)

This does not work - at least not in the intended way. What this does is it
will replace all occurrences of the regexp with the string representation
of "subs":

irb(main):018:0> body = "foo bar"
=> "foo bar"
irb(main):019:0> body.gsub(/(?=\b).+(?=\b)/, subs)
=> "{\"you\"=>\"he\", \"up\"=>\"down\", \"in\"=>\"out\"}"

There are several problems here:

1. the regexp does not match words but arbitrary long sequences of words.
2. the default proc uses the first argument which happens to be the Hash
3. the string replacement as indicated above.

Here's a more appropriate version

irb(main):026:0> subs.default_proc = lambda {|h,k| k}
=> #<Proc:0x8022628c@(irb):26 (lambda)>
irb(main):027:0> body.gsub(/\w+/) {|m| subs[m]}
=> "foo bar"
irb(main):028:0> body = "I am you"
=> "I am you"
irb(main):029:0> body.gsub(/\w+/) {|m| subs[m]}
=> "I am he"

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

In This Thread