New to Ruby. Specific syntax question

I'm reading through the PickAxe book right now. I'm new to programming,
and more specifically, Ruby.

Right now I'm looking at a line that says this:
fib_up_to(1000) {|f| print f, ""}

I'm curious about the parts of this statement.

I know that "fib_up_to(1000)" passes 1000 into this specific method.

The thing that gets me confused is the block statement (right
terminology?). What's the "|f|"? I know "print f" says to print this
variable. Then, what're the quotes for?

Is a variable "f" created in this block, then told to print, and more
specifically, told to print into the parentheses?

I'm new, so sorry if this is a hang-up. I just want to understand.

Thank you.

···

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

it's not a variable create in this block, but a variable passed into
the block

take an example:
def fib_up_to(max)
....
yield t
...
end

fib_up_to(1000) {|f| print f}

the f in block is the t in each step

···

On Jul 16, 12:20 pm, Cole Mister <chon...@gmail.com> wrote:

I'm reading through the PickAxe book right now. I'm new to programming,
and more specifically, Ruby.

Right now I'm looking at a line that says this:
fib_up_to(1000) {|f| print f, ""}

I'm curious about the parts of this statement.

I know that "fib_up_to(1000)" passes 1000 into this specific method.

The thing that gets me confused is the block statement (right
terminology?). What's the "|f|"? I know "print f" says to print this
variable. Then, what're the quotes for?

Is a variable "f" created in this block, then told to print, and more
specifically, told to print into the parentheses?

I'm new, so sorry if this is a hang-up. I just want to understand.

Thank you.
--
Posted viahttp://www.ruby-forum.com/.

take an example:
def fib_up_to(max)
....
yield t
...
end

fib_up_to(1000) {|f| print f}

the f in block is the t in each step

Okay. That makes sense.

The block is pretty much defining the yield statement?

It'll make more and more sense the farther I get into the book I hope.

Thanks.

···

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

Hi --

···

On Wed, 16 Jul 2008, Cole Mister wrote:

> take an example:

def fib_up_to(max)
....
yield t
...
end

fib_up_to(1000) {|f| print f}

the f in block is the t in each step

Okay. That makes sense.

The block is pretty much defining the yield statement?

It'll make more and more sense the farther I get into the book I hope.

The block is a snippet of executable code. The yield command executes
it; in other words, by supplying the code block, you're giving the
method some code that it can execute. You call the method, and the
method calls you back :slight_smile:

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

Cole Mister wrote:

The block is pretty much defining the yield statement?

In Ruby, code blocks are very important, but they're often a sticking
point for newcomers (myself included) until you realise what they're
doing.

Perhaps the easiest way to understand them is as anonymous subroutines.
For example,

  { |f| puts f }

is something (but not exactly!) like:

  def nameless(f)
    puts f
  end

so anything you pass to nameless() gets printed. (Actually code blocks
are much more than this, as they can carry around their local
environment with them and be used as closures, but this should give you
the general idea.)

You can now use iterators to do useful things like this:

  (1 .. 4).each { |f| puts f }

and 1, 2, 3, 4 will be fed in turn to the block. So the output will be

  1
  2
  3
  4

You can give a code block a life of its own, using lambda:

  x = lambda { |f| puts f }

and you can pass this Proc object x to methods, etc etc. Very useful
once you get used to the idea.

Disclaimer: this is not the truth, the whole truth, and nothing but the
truth, so please don't shoot me down in flames! "Education is a process
of diminishing deception."

Dave

···

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

Thanks everyone.

Does this make more sense the more you use and play around with Ruby?

···

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

Definitely. I'd say don't worry about how code blocks work until you
need to. Just follow the examples of how upto works and stick with it
- it'll click someday, just play!

···

On Wed, Jul 16, 2008 at 3:34 PM, Cole Mister <chonald@gmail.com> wrote:

Thanks everyone.

Does this make more sense the more you use and play around with Ruby?

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

--
Paul Smith
paul@pollyandpaul.co.uk

I think it's more important to know how it works to play it better
it will click someday, haha

To Cole Mister:
I am a newbie too,here is my MSN:ikari.shinji.eva@gmail.com, and we
can learn together, ^^

···

On Jul 16, 10:40 pm, Paul Smith <p...@pollyandpaul.co.uk> wrote:

Definitely. I'd say don't worry about how code blocks work until you
need to. Just follow the examples of how upto works and stick with it
- it'll click someday, just play!