An each/block problem

Hi, I want to take the value from an each method and place it in a
function call called borrowed, that runs an evaluation on the value
'bor'.
It's plain to see that 'bor' is undefined outside the block. Is there a
way around this?

In simple terms I want to get the value of a certain attribute for each
user and run that value in a function

out = User.find(:all)

out.each {|bor| bor.books_borrowed}

borrowed.call(bor)

···

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

It's plain to see that 'bor' is undefined outside the block. Is there a
way around this?

Define it outside the block. :wink:

In simple terms I want to get the value of a certain attribute for each
user and run that value in a function

out = User.find(:all)

bor = Borrowed.new # Substitute your actual code, obviously.

···

On Sat, Nov 13, 2010 at 4:29 PM, Paul Roche <prpaulroche@gmail.com> wrote:

out.each {|bor| bor.books_borrowed}

borrowed.call(bor)

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

Here's more explantion.......

I have a table called User which has a books_borrowed column. I'd like
to stick to this lambda if at all possible :slight_smile:

borrowed = lambda do |x|
if x > 1 then p "you have borrowed too many"
else puts "you can borrow more"
  end
end

out = User.find(:all)
out.each {|bor| bor.books_borrowed}

borrowed.call(bor)

···

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

Just one more thing. if I want to attach the 'name' of the User to that.
How can I do it?

i.e so the print out is "User1: you can borrow more"

···

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

Phillip Gawlowski wrote in post #961200:

It's plain to see that 'bor' is undefined outside the block. Is there a
way around this?

Define it outside the block. :wink:

but for an 'each' don't I need a block?

In simple terms I want to get the value of a certain attribute for each
user and run that value in a function

out = User.find(:all)

bor = Borrowed.new # Substitute your actual code, obviously.

out.each {|bor| bor.books_borrowed}

borrowed.call(bor)

Sorry, could explaini this a bit more.

···

On Sat, Nov 13, 2010 at 4:29 PM, Paul Roche <prpaulroche@gmail.com> > wrote:

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

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

Wouldn't that just be:

  users = User.all
  users.each {|user| borrowed.call(user.books_borrowed) }

?

···

On Sat, Nov 13, 2010 at 7:46 AM, Paul Roche <prpaulroche@gmail.com> wrote:

I have a table called User which has a books_borrowed column. I'd like
to stick to this lambda if at all possible :slight_smile:

borrowed = lambda do |x|
if x > 1 then p "you have borrowed too many"
else puts "you can borrow more"
end
end

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

Pass the entire User object, e.g. `borrowed.call(user)` and change
your proc to:

borrowed = lambda do |user|
if user.books_borrowed > 1 then p "#{user.name}: you have borrowed too many"
else puts "#{user.name}: you can borrow more"
end
end

or have it take 2 arguments, user.name and user.books_borrowed,
exercise left to the reader :slight_smile:

HTH,

···

On Sat, Nov 13, 2010 at 8:08 AM, Paul Roche <prpaulroche@gmail.com> wrote:

Just one more thing. if I want to attach the 'name' of the User to that.
How can I do it?

i.e so the print out is "User1: you can borrow more"

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

Hassan Schroeder wrote in post #961207:

···

On Sat, Nov 13, 2010 at 7:46 AM, Paul Roche <prpaulroche@gmail.com> > wrote:

I have a table called User which has a books_borrowed column. I'd like
to stick to this lambda if at all possible :slight_smile:

borrowed = lambda do |x|
if x > 1 then p "you have borrowed too many"
else puts "you can borrow more"
end
end

Wouldn't that just be:

  users = User.all
  users.each {|user| borrowed.call(user.books_borrowed) }

?

Indeed you're right. I didn't think of trying it, because I thought that
borrowed.call wouldn't be accessable in the block. Thanks for that.
Infact this is the complete working code.......

users = User.find(:all)
  users.each {|user| borrowed.call(user.books_borrowed) }

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

but for an 'each' don't I need a block?

Yes, you do. Which is why you declare your variables outside of a
block if you need access to them after the block is done.

For example:
bor =

[1,2,3].each {|num| bor[num] = Time.now }

puts bor

Sorry, could explaini this a bit more.

I hope the above explanation helps. Also, I made a mistake: I should
have changed your block variable (the bit between the |) to something
else, otherwise Ruby 1.9 complains.

P.S.: Please trim your quotes. :slight_smile:

···

On Sat, Nov 13, 2010 at 4:42 PM, Paul Roche <prpaulroche@gmail.com> wrote:
--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

Hassan Schroeder wrote in post #961211:

Just one more thing. if I want to attach the 'name' of the User to that.
How can I do it?

i.e so the print out is "User1: you can borrow more"

Pass the entire User object, e.g. `borrowed.call(user)` and change
your proc to:

borrowed = lambda do |user|
if user.books_borrowed > 1 then p "#{user.name}: you have borrowed too
many"
else puts "#{user.name}: you can borrow more"
end
end

or have it take 2 arguments, user.name and user.books_borrowed,
exercise left to the reader :slight_smile:

HTH,

Thanks for that. I am arranging to take 2 parameters...

users = User.find(:all)
  users.each {|namex, user| nameb.call(namex.name)
borrowed.call(user.books_borrowed) }

I get the following error....

books.rb:80: syntax error, unexpected ',', expecting '}'users.each
{|namex,user| (nameb.call(namex.name)),
(borrowed.call(user.books_borrowed)) }

ps Thanks Phillip Gawlowski for that detail :slight_smile:

···

On Sat, Nov 13, 2010 at 8:08 AM, Paul Roche <prpaulroche@gmail.com> > wrote:

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

I can't even begin to guess what you think should happen here -- what
in the heck is "namex"? :slight_smile:

What I was implying was that your 'borrowed' proc take 2 arguments,
e.g. borrowed.call(user.name, user.books_borrowed) if you don't want
to pass the entire user object to it.

···

On Sat, Nov 13, 2010 at 9:33 AM, Paul Roche <prpaulroche@gmail.com> wrote:

Thanks for that. I am arranging to take 2 parameters...

users = User.find(:all)
users.each {|namex, user| nameb.call(namex.name)
borrowed.call(user.books_borrowed) }

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan

Hassan Schroeder wrote in post #961225:

···

On Sat, Nov 13, 2010 at 9:33 AM, Paul Roche <prpaulroche@gmail.com> > wrote:

Thanks for that. I am arranging to take 2 parameters...

users = User.find(:all)
users.each {|namex, user| nameb.call(namex.name)
borrowed.call(user.books_borrowed) }

I can't even begin to guess what you think should happen here -- what
in the heck is "namex"? :slight_smile:

What I was implying was that your 'borrowed' proc take 2 arguments,
e.g. borrowed.call(user.name, user.books_borrowed) if you don't want
to pass the entire user object to it.

Ah yes, I see whay you mean :slight_smile:

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