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 <lists / ruby-forum.com>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/.
>
>