an each/block problem

P

Paul Roche

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)
 
P

Phillip Gawlowski

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

Define it outside the 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 =3D User.find:)all)
bor =3D Borrowed.new # Substitute your actual code, obviously.
out.each {|bor| =A0bor.books_borrowed}

borrowed.call(bor)


--=20
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.
 
P

Paul Roche

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 :)




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)
 
H

Hassan Schroeder

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


borrowed =3D lambda do |x|
=A0if x > 1 then p "you have borrowed too many"
=A0else puts "you can borrow more"
=A0end
end

Wouldn't that just be:

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

?
--=20
Hassan Schroeder ------------------------ (e-mail address removed)
twitter: @hassan
 
P

Paul Roche

Hassan Schroeder wrote in post #961207:
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) }
 
P

Paul Roche

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"
 
H

Hassan Schroeder

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 :)

HTH,
 
P

Phillip Gawlowski

=A0but 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 =3D []

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

puts bor
=A0Sorry, 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. :)
--=20
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.
 
P

Paul Roche

Hassan Schroeder wrote in post #961211:
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 :)

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 :)
 
H

Hassan Schroeder

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

users =3D User.find:)all)
=A0users.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"? :)

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.

--=20
Hassan Schroeder ------------------------ (e-mail address removed)
twitter: @hassan
 
P

Paul Roche

Hassan Schroeder wrote in post #961225:
I can't even begin to guess what you think should happen here -- what
in the heck is "namex"? :)

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top