# New to Ruby. Specific syntax question

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192>\
**Category:** Ruby\
**Created:** [July 16, 2008, 6:25am UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192 "2008-07-16T06:25:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 6:25am UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/1 "2008-07-16T06:25:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 9:21am UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/2 "2008-07-16T09:21:10Z")

</div>

On Jul 16, 12:20 pm, Cole M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/3 "2008-07-16T14:40:10Z")

</div>

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 🙂

David

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/4 "2008-07-16T14:36:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/5 "2008-07-16T16:39:09Z")

</div>

Thanks everyone.

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

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/6 "2008-07-16T14:54:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 16, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/7 "2008-07-16T16:45:20Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![chonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e9c0ed/32.png) [@chonald](https://www.ruby-forum.com/u/chonald)\
**Post date:** [July 17, 2008, 4:06am UTC](https://www.ruby-forum.com/t/new-to-ruby-specific-syntax-question/143192/8 "2008-07-17T04:06:10Z")

</div>

On Jul 16, 10:40 pm, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:removed\_email\_address@domain.invalid, and we  
can learn together, ^^
