Difficulty with blocks

Lähettäjä: Johannes Ahl-mann <softpro@gmx.net>
Aihe: Re: difficulty with blocks

hi,

sorry for answering my own post, but i am adopting ruby at an incredible
rate. i know python quite well and it took me literally mere minutes to
get the BASIC hang of blocks... i've been programming in ruby for 3
hours now and it already feels like home! not much cosier than python,
but the block closures and the possibility of continuations drags me
into ruby nonetheless :wink:

well, i've got a rather satisfactory solution to my problem and would now like
to ask whether my implementation can be considered "the ruby way" or if it
feels rather strange to the senior rubiists (i.e. people who have
programmed in ruby for more than 3 hours) *ggg*

### snip ###

def fib_block &block
  a, b = 1, 1
  while true:
    block.call(a)
    a, b = b, a+b
  end
end

require 'generator'
def fibUpTo n, &block
  fibs = Generator.new do |g|
    fib_block {|val| g.yield val}
  end

  while (f = fibs.next()) < n
    block.call(f)
  end
end

def into(anArray)
  return proc {|val| anArray << val}
end
fibUpTo 20, &into(a = )
puts a

### snip ###

thx and forgive me for answering my own questions. i really shouldn't do
that ;-))

That works fine, but let me ask you what you're trying to do?
There're certainly much more natural ways to get a Fibonacci
abstracted in Ruby -and this is not said maliciously in any way;
it'd just be a lot easier to advise if we knew the goal, which
I assume may not be abstracting the FibSeq but to work out a
certain idiom? :slight_smile:

Johannes

P.S.: what is it with the implicit blocks in ruby. i am aware that many
people seem not to be explicitely passing the block into a function, but
i really find that distracting and too much of a perl idiom. is my way
alright, or was it unidiomatic??

It's just shorter to write.

P.P.S: is there a better way to write "fib_block {|val| g.yield val}"?
maybe something like "fib_block.apply(g.yield)" or so. i guess not, but
the repetition bothers me!

E

That works fine, but let me ask you what you're trying to do?
There're certainly much more natural ways to get a Fibonacci
abstracted in Ruby -and this is not said maliciously in any way;
it'd just be a lot easier to advise if we knew the goal, which
I assume may not be abstracting the FibSeq but to work out a
certain idiom? :slight_smile:

i am really just trying to come up to speed in ruby. i was wondering how
a natural idiom for "separating mechanism and policy" would look in
python. in another post someone wrote a really short version using
"break" in a block... i didn't know this was possible and is pretty much
what i was looking for.

the world of internal iterators is just a little different from the
external ones in python. but after spending a little more time with
ruby, finding the Generator module and being told that you can "break"
within a module (although i haven't quite grasped how that fits in with
the closure concept!?) i am a lot more comfortable with internal
iterators...

idioms take a long to time to learn, comprehend and apply...

thx,

Johannes