sorry just was learning ruby so have some low level questions,
in this statement:
1.upto(10) do |c| print c," " end
what's the usage of "| ... |" in ruby? can't easily understand.
Thanks.
sorry just was learning ruby so have some low level questions,
in this statement:
1.upto(10) do |c| print c," " end
what's the usage of "| ... |" in ruby? can't easily understand.
Thanks.
Sunday, November 15, 2009, 5:59:31 AM, you wrote:
sorry just was learning ruby so have some low level questions,
in this statement:
1.upto(10) do |c| print c," " end
what's the usage of "| ... |" in ruby? can't easily understand.
Thanks.
I'm just learning Ruby, too .. and I found this fairly tough to wrap
my head around. This is what I think I know:
Almost EVERYTHING in Ruby is an object. Thus the "1" in "1.upto(10)" is an
object.
Object have methods that can be applied to them. Or, equivalently,
objects can have messages sent to them. "upto" is method that can be
applied to the integer 1.
"upto" tokes an argument "10".
- - -
Now here's where I get vaguer.
"upto" is a method that, in turn, can call subroutines. One of the
subroutines it can call is ...
do |c| print c," " end
The object.method known as 1.upto(10) will call the subroutine
do |c| print c," " end
ten times.
Each time it calls
do |c| print c," " end
it will pass a parameter to the subroutine. In this case it will pass
1 the first time, 2 the second time ... 10 the last time.
The subroutine
do |c| print c," " end
will see that parameter as c
Thus, the subroutine
do |c| print c," " end
has to know a fair bit about the method "upto" in order to know what
"upto" will pass to the subroutine. That is, it has to know a fair
bit about the public interface of "upto" and what "upto" is supposed
to do. Internal implementation of "upto" is, of course, nearly
irrelevant.
Like in most high level languages, the names of the parameters and the
arguments do not have to be the same. Thus,
do |c| print c," " end
is exactly equivalent to
do |parm| print parm," " end
I hope this helps.
duxieweb wrote:
sorry just was learning ruby so have some low level questions,
in this statement:
1.upto(10) do |c| print c," " end
what's the usage of "| ... |" in ruby? can't easily understand.
Thanks.
The topic you want to read about is "blocks".
In ruby, upto() is an iterator, which is something that produces one
value at a time. To "catch" those values, you specify what's called a
"block", which is like writing a method definition right next to the
call to the upto() iterator:
1.upto(10) {|num| puts num}
ruby then assigns each value produced by the iterator to the block's
parameter variable.
The longer multi line syntax looks like this:
1.upto(10) do |num|
puts num
puts num * 2
end
Generally, you use the short syntax when there is only one line to
execute inside your "method" definition". If there is more than one
line inside the "method", you use the multiline form which requires that
you insert 'do' between the call to the iterator and the block. There
is one slight difference between the shorter syntax and the longer
syntax, but it is unimportant at this point.
--
Posted via http://www.ruby-forum.com/\.
It's the lazy execution boolean or operator
x1 = nil
x2 = [1,2,3]
x3 = [4,5,6]
a = x1 || x2.push(4, 5)
b = x3 || x2.push(6, 7)
# assignment operator version often used to initialize a variable if it
doesn't already have a value
@var ||= 'something'
@var ||= 'whatevs'
after wards:
a == x2 == [1, 2, 3, 4, 5]
b == x3 == [4, 5, 6]
@var == 'something'
On Sunday November 15 2009 7:59:31 am duxieweb wrote:
sorry just was learning ruby so have some low level questions,
in this statement:
1.upto(10) do |c| print c," " end
what's the usage of "| ... |" in ruby? can't easily understand.
Thanks.
Oh lawd i'm a total jackass for not realizing there was more to the e-mail.
Disregard the previous nonsense. | | is used to indicate a block variable. Ya
gotta read the pickaxe book or another ruby guide for more on what that means:
http://www.ruby-doc.org/docs/ProgrammingRuby/
On Sunday November 15 2009 7:59:31 am duxieweb wrote:
sorry just was learning ruby so have some low level questions,
in this statement:
1.upto(10) do |c| print c," " end
what's the usage of "| ... |" in ruby? can't easily understand.
Thanks.
Ralph Shnelvar wrote:
Sunday, November 15, 2009, 5:59:31 AM, you wrote:
> sorry just was learning ruby so have some low level questions,
> in this statement:
> 1.upto(10) do |c| print c," " end
> what's the usage of "| ... |" in ruby? can't easily understand.
It declares block arguments. See the sections in the Pickaxe Book on
blocks.
> Thanks.
I'm just learning Ruby, too .. and I found this fairly tough to wrap
my head around. This is what I think I know:Almost EVERYTHING in Ruby is an object. Thus the "1" in "1.upto(10)" is
an
object.
Right.
Object have methods that can be applied to them. Or, equivalently,
objects can have messages sent to them. "upto" is method that can be
applied to the integer 1."upto" tokes an argument "10".
Right.
- - -
Now here's where I get vaguer.
"upto" is a method that, in turn, can call subroutines.
Blocks -- "subroutine" is not a Ruby term.
One of the
subroutines it can call is ...
do |c| print c," " end
It can call any block at all.
The object.method known as 1.upto(10) will call the subroutine
do |c| print c," " end
ten times.Each time it calls
do |c| print c," " end
it will pass a parameter to the subroutine. In this case it will pass
1 the first time, 2 the second time ... 10 the last time.
Correct.
The subroutine
do |c| print c," " end
will see that parameter as c
Also correct.
Thus, the subroutine
do |c| print c," " end
has to know a fair bit about the method "upto" in order to know what
"upto" will pass to the subroutine.
No. The block has to know that upto will yield it one object. That's
all it has to know.
That is, it has to know a fair
bit about the public interface of "upto" and what "upto" is supposed
to do.
Nope. The block expects one object. It doesn't really care whether
it's being yielded to from 1.upto(10), or ['e', 'd', :c, {:b =>
3957}].each, or anything else that will yield it one object (of any
kind) as an argument. In fact, you could store the block in a variable
and yield to it from each of those methods in turn.
Internal implementation of "upto" is, of course, nearly
irrelevant.
Totally irrelevant, actually.
Like in most high level languages, the names of the parameters and the
arguments do not have to be the same. Thus,
do |c| print c," " end
is exactly equivalent to
do |parm| print parm," " end
You're right that those are equivalent, but it has nothing to do with
your statement about parameters and arguments.
I hope this helps.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.
7stud -- wrote:
duxieweb wrote:
ruby then assigns each value produced by the iterator to the block's
parameter variable...
which is delimited with the |....| you asked about!
--
Posted via http://www.ruby-forum.com/\.
Block syntax is also clearly explained in "The Ruby Programming
Language" by Flanagan & Matsumoto in section 5.4.1.
Kyle wrote:
Oh lawd i'm a total jackass for not realizing there was more to the
e-mail.
Disregard the previous nonsense. | | is used to indicate a block
variable. Ya
gotta read the pickaxe book or another ruby guide for more on what that
means:
--
Posted via http://www.ruby-forum.com/\.
Thanks all.
I was reading O'Reilly Learning Ruby, is this a recommended book?
2009/11/16 Steve Wilhelm <steve@studio831.com>:
Block syntax is also clearly explained in "The Ruby Programming
Language" by Flanagan & Matsumoto in section 5.4.1.
Thanks all.
I was reading O'Reilly Learning Ruby, is this a recommended book?>
Yes I highly recommend that you buy and read it from end-to-end. Explanations are clear and succinct. BTW you may also want to read up ion 'yield' as this is what iterators use to return a value to a receiving block of code if one exists. Again, the O'Reilly book will explain all.
Imran Nazir
Imran's profile" border="0">
Friend, Boho, House Owner, Citizen, Engineer
________________________________
From: duxieweb <duxieweb@gmail.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Mon, 16 November, 2009 6:37:29
Subject: Re: what's ||
2009/11/16 Steve Wilhelm <steve@studio831.com>:
Block syntax is also clearly explained in "The Ruby Programming
Language" by Flanagan & Matsumoto in section 5.4.1.
Thanks all.
I was reading O'Reilly Learning Ruby, is this a recommended book?