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.

On Jul 16, 12:20 pm, Cole M. [email protected] wrote:

The thing that gets me confused is the block statement (right
Posted viahttp://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

Hi –

On Wed, 16 Jul 2008, Cole M. wrote:

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

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.

Thanks everyone.

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

Cole M. 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

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 Jul 16, 10:40 pm, Paul S. [email protected] 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!

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

To Cole M.:
I am a newbie too,here is my MSN:[email protected], and we
can learn together, ^^

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs