New to Ruby. Specific syntax question.

C

Cole Mister

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

Tachikoma

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.

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
 
C

Cole Mister

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

David A. Black

Hi --

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
 
D

Dave Bass

Cole said:
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
 
C

Cole Mister

Thanks everyone.

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

Paul Smith

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!
 
T

Tachikoma

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 Mister:
I am a newbie too,here is my MSN:[email protected], and we
can learn together, ^^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top