[#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: Brandon Weaver <keystonelemur@...>
Date: 2013-07-26 05:07:37 UTC
List: ruby-talk #409121
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/.
>
>

In This Thread