an each/block problem

Discussion in 'Ruby' started by Paul Roche, Nov 13, 2010.

  1. Paul Roche

    Paul Roche Guest

    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/.
    Paul Roche, Nov 13, 2010
    #1
    1. Advertising

  2. On Sat, Nov 13, 2010 at 4:29 PM, Paul Roche <> wrote:
    >
    > 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.
    Phillip Gawlowski, Nov 13, 2010
    #2
    1. Advertising

  3. Paul Roche

    Paul Roche Guest

    Phillip Gawlowski wrote in post #961200:
    > On Sat, Nov 13, 2010 at 4:29 PM, Paul Roche <>
    > wrote:
    >>
    >> It's plain to see that 'bor' is undefined outside the block. Is there a
    >> way around this?

    >
    > Define it outside the block. ;)



    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.

    >
    > --
    > 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/.
    Paul Roche, Nov 13, 2010
    #3
  4. Paul Roche

    Paul Roche Guest

    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)

    --
    Posted via http://www.ruby-forum.com/.
    Paul Roche, Nov 13, 2010
    #4
  5. On Sat, Nov 13, 2010 at 7:46 AM, Paul Roche <> 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 :)
    >
    >
    > 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 ------------------------
    twitter: @hassan
    Hassan Schroeder, Nov 13, 2010
    #5
  6. Paul Roche

    Paul Roche Guest

    Hassan Schroeder wrote in post #961207:
    > On Sat, Nov 13, 2010 at 7:46 AM, Paul Roche <>
    > 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 :)
    >>
    >>
    >> 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/.
    Paul Roche, Nov 13, 2010
    #6
  7. Paul Roche

    Paul Roche Guest

    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/.
    Paul Roche, Nov 13, 2010
    #7
  8. On Sat, Nov 13, 2010 at 8:08 AM, Paul Roche <> 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"


    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,
    --
    Hassan Schroeder ------------------------
    twitter: @hassan
    Hassan Schroeder, Nov 13, 2010
    #8
  9. On Sat, Nov 13, 2010 at 4:42 PM, Paul Roche <> wrote:
    >
    > =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.
    Phillip Gawlowski, Nov 13, 2010
    #9
  10. Paul Roche

    Paul Roche Guest

    Hassan Schroeder wrote in post #961211:
    > On Sat, Nov 13, 2010 at 8:08 AM, Paul Roche <>
    > 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"

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

    --
    Posted via http://www.ruby-forum.com/.
    Paul Roche, Nov 13, 2010
    #10
  11. On Sat, Nov 13, 2010 at 9:33 AM, Paul Roche <> wrote:

    > 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 ------------------------
    twitter: @hassan
    Hassan Schroeder, Nov 13, 2010
    #11
  12. Paul Roche

    Paul Roche Guest

    Hassan Schroeder wrote in post #961225:
    > On Sat, Nov 13, 2010 at 9:33 AM, Paul Roche <>
    > 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"? :)
    >
    > 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 :)

    --
    Posted via http://www.ruby-forum.com/.
    Paul Roche, Nov 13, 2010
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Showjumper
    Replies:
    1
    Views:
    688
    Showjumper
    Mar 19, 2005
  2. John Blair
    Replies:
    1
    Views:
    396
    Eliyahu Goldin
    Aug 3, 2005
  3. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    416
    Tjerk Wolterink
    Nov 3, 2004
  4. morrell
    Replies:
    1
    Views:
    933
    roy axenov
    Oct 10, 2006
  5. Pat Maddox
    Replies:
    6
    Views:
    141
    Marcin Mielżyński
    Jan 20, 2006
Loading...

Share This Page