Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9229179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:30:29+00:00 2026-06-18T05:30:29+00:00

CODE-I def sample x = hi puts #{x} x = yield puts #{x} end

  • 0

CODE-I

def sample
  x = "hi"
  puts " #{x}"
  x = yield
  puts " #{x}"
end

In the below code block {} from here => sample {"hellooo"} called
yield and assigned “hellooo” to x. Looks good and as expected.

sample{'helloo'}
# >>  hi
# >>  helloo

CODE-II

o = Object.new
def o.each
  x = yield
  p x
  x = yield
  p x
  x = yield
  p x
end
e = o.to_enum # => #<Enumerator: #<Object:0x007fd1d20494e8>:each>

Why the same not happened in the below call with e.next "sample", as the p didn’t printed anything?

e.next {"sample"} # => nil
e.next # => nil
# >> nil

EDIT (Here how enum#feed did the change with the help of yield?)

o = Object.new
=> #<Object:0x2299d88>
def o.each
x = yield         
p x       
x = yield
p x 
x = yield
p x 
end
=> nil
e=o.to_enum
=> #<Enumerator: #<Object:0x2299d88>:each>
e.next
=> nil
e.feed "hi"
=> nil
e.next
"hi"
=> nil
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:30:30+00:00Added an answer on June 18, 2026 at 5:30 am

    next does not take a block. So if you pass it one, it simply ignores it.

    It is not possible to simulate something being returned from the block when using the next method of an enumerator. When using an to_enum, the block given to the each method will always return nil except if a value has previously supplied by the feed method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's a sample code: def foo(): def bar(): foobar = 'foobaz' foobar = 'foobar'
Here's a short sample of my code: def update_records! # Teams. home_team_record = PoolRecord.for_recordable_and_user(event_home_team,
I have the following code: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
Here is the code I'm working with: class Trader def initialize(ticker =GLD) @ticker =
I have a following simple code: def get(): return [lambda: i for i in
I have this simple code in my user_controller.rb file #listing all users def index
My code: def power(x: Double, n: Int): Double = { if (n % 2
I have this code: def __init__(self, a, b, c, d...): self.a = a self.b
i have this code: def some_method(): #i need to get some attrs :( return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.