From: Brandon Weaver Date: 2013-07-26T14:07:37+09:00 Subject: Re: replace words using gsub --047d7b672038ed1df304e2631e65 Content-Type: text/plain; charset=ISO-8859-1 The k is an iterator. Let's use another example here to demonstrate: 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. 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) } ...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. 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.comas well later, it should help. Cheers On Thu, Jul 25, 2013 at 10:56 PM, noterrain mee wrote: > New to ruby! Body is a string, i am using the code directly below. I am > not quite understand what does |k| and K means. This code shows the > error "undefined method `default_proc=' for {"up"=>"down", "in"=>"out", > "you"=>"he"}:Hash (NoMethodError)". Could someone help me out? > > subs = { > "you" => "he", > "up" => "down", > "in" => "out"} > > subs.default_proc = proc {|k| k} > body.gsub(/(?=\b).+(?=\b)/, subs) > > -- > Posted via http://www.ruby-forum.com/. > > --047d7b672038ed1df304e2631e65 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The k is an iterator. Let's use another example here to demonstrate:
array =3D [1,2,3]
array.each{ |i| puts i * i }
<= br> 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 i= s set to 1 and prints 1, then is set to 2 and prints 4, then is set to 3 an= d prints 9. What you are doing is passing each element to an anonymous func= tion called a block, which comes from some functional type programming with= Lambda.

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.)
<= br>
Let's say we have string:
string =3D "you up in there"

...and subs as you specified it. We need to iterate thr= ough the subs hash, so we do as so:
subs.each_pair{ |k, v| string.gsub!(k,v) }

...but why are there TWO iterators there? Who said there was a l= imit? 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) th= at we use for substitution. You will notice that I use the gsub! method ins= tead 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 obj= ect. There are ways to prevent side effects which may very well lead to cle= aner code, and it will be worthwhile to look into this.

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

Cheers

On Thu,= Jul 25, 2013 at 10:56 PM, noterrain mee <lists@ruby-forum.com><= /span> wrote:
New to ruby! Body is a string, i am using th= e code directly below. I am
not quite understand what does |k| and =A0K means. =A0This code shows the error "undefined method `default_proc=3D' for {"up"=3D&g= t;"down", "in"=3D>"out",
"you"=3D>"he"}:Hash (NoMethodError)". Could som= eone help me out?

subs =3D {
=A0 =A0 "you" =3D> "he",
=A0 =A0 "up" =3D> "down",
=A0 =A0 "in" =3D> "out"}

subs.default_proc =3D proc {|k| k}
body.gsub(/(?=3D\b).+(?=3D\b)/, subs)

--
Posted via http://= www.ruby-forum.com/.


--047d7b672038ed1df304e2631e65--