[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

|| explanation in ruby... in pseudolanguage

Tuka Opaleye

10/8/2006 10:21:00 PM

Hi,

I am trying to get my head around the || symbol used extensively in
ruby. To me it is somewhat foreign still and I am trying a few exercise
to make it click. Can anyone suggest a phrase (pseudolanguage) that
accurately expresses the possible expressions using || ?

For example, see the comments and please correct me if I am wrong. Could
you add other cases that I may not have caught here ?

input.each_byte do |b|
case b
when ?\C-c; puts 'Control-C: stopped a process?'
when ?\C-z; puts 'Control-Z: suspended a process?'
when ?\n; puts 'Newline.'
when ?\M-x; puts 'Meta-x: using Emacs?'
end
for each byte in input, assign it to b...



Ex 2:
open('smiley.html', 'wb') do |f|
f << '<meta http-equiv="Content-Type"
content="text/html;charset=UTF-8">'
f << "\xe2\x98\xBA"
end

Sugestions ?.....


Ex 3:
octal = "\000\001\010\020"
octal.each_byte { |x| puts x }
for each byte in octal, assign an x...

TIA,
Tuka

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

15 Answers

Tim Hunter

10/8/2006 11:26:00 PM

0

Tuka Opaleye wrote:
> Hi,
>
> I am trying to get my head around the || symbol used extensively in
> ruby. To me it is somewhat foreign still and I am trying a few exercise
> to make it click. Can anyone suggest a phrase (pseudolanguage) that
> accurately expresses the possible expressions using || ?
>
> For example, see the comments and please correct me if I am wrong. Could
> you add other cases that I may not have caught here ?
>
> input.each_byte do |b|
> case b
> when ?\C-c; puts 'Control-C: stopped a process?'
> when ?\C-z; puts 'Control-Z: suspended a process?'
> when ?\n; puts 'Newline.'
> when ?\M-x; puts 'Meta-x: using Emacs?'
> end
> for each byte in input, assign it to b...
>
>
>
> Ex 2:
> open('smiley.html', 'wb') do |f|
> f << '<meta http-equiv="Content-Type"
> content="text/html;charset=UTF-8">'
> f << "\xe2\x98\xBA"
> end
>
> Sugestions ?.....
>
>
> Ex 3:
> octal = "\000\001\010\020"
> octal.each_byte { |x| puts x }
> for each byte in octal, assign an x...
>
> TIA,
> Tuka
>
>
In these examples the || delimit the argument list to the block, similar
to the way parentheses delimit the argument list in a method definition.
When the method yields to the block it passes some value or values from
the method to the block:

yield(arg1, arg2, arg3)

and these values become the block arguments:

obj.meth { |a, b, c| ....}

In the above case arg1 is the 'a' argument, 'arg2' is the 'b' argument,
and 'arg3' is the 'c' argument. A method may yield multiple times,
passing a new set of arguments each time, as each_byte does in examples
1 and 3, or it may yield only once, as in example 2.


Dave Burt

10/9/2006 2:01:00 AM

0

Tuka Opaleye wrote:
> I am trying to get my head around the || symbol used extensively in
> ruby. To me it is somewhat foreign still and I am trying a few exercise
> to make it click. Can anyone suggest a phrase (pseudolanguage) that
> accurately expresses the possible expressions using || ?
>
> input.each_byte do |b|
> case b
> ...
> end
> for each byte in input, assign it to b...

You're right, assignment is correct. Imagine a special variable
yielded_values; the block could be expressed like this:

do
b = *yielded_values
case b
...
end
end

Implicit in this is that you can use full assignment semantics, as in
this idiom:

my_hash.inject(initial_value) do |memo, (key, value)|
...
end

It works this way: inject yields a [memo, whatever_each_yields] pair,
and Hash#each yields a [key, value] pair. This is the analogous assignment:

yielded_values = [:memo, [:key, :value]]
memo, (key, value) = *yielded_values

How would you say this in English? Inject is problematic this way; I
don't understand how to explain it naturally using the word "inject" at
all. But perhaps you could use a general form such as "call inject with
the parameter initial_value and a block taking parameters memo, key and
value."

> Ex 2:
> open('smiley.html', 'wb') do |f|
> f << ...
> end
>
> Sugestions ?.....

*With* an open binary-write mode file handle to "smiley.html" *as* f...

What a block means depends on the method. You've looked at iteration and
scope-of-an-opened-object examples, which are probably the most common,
but there are others (e.g. Markaby).

Cheers,
Dave

Tuka Opaleye

10/9/2006 5:13:00 PM

0

Thanks for the input guys. This really helps.

Sorry about the confusion with OR, I forgot that || actually means OR in
most languages.

I have heard |var| called goal-post notation (I think it was in a ROR
podcast...) - is that what you call it ? I owuld guess another valid
name is something with the word "pipes".

Cheers,
Tuka


Dave Burt wrote:
> Tuka Opaleye wrote:
>> I am trying to get my head around the || symbol used extensively in
>> ruby. To me it is somewhat foreign still and I am trying a few exercise
>> to make it click. Can anyone suggest a phrase (pseudolanguage) that
>> accurately expresses the possible expressions using || ?
>>
>> input.each_byte do |b|
>> case b
>> ...
>> end
>> for each byte in input, assign it to b...
>
> You're right, assignment is correct. Imagine a special variable
> yielded_values; the block could be expressed like this:
>
> do
> b = *yielded_values
> case b
> ...
> end
> end
>
> Implicit in this is that you can use full assignment semantics, as in
> this idiom:
>
> my_hash.inject(initial_value) do |memo, (key, value)|
> ...
> end
>
> It works this way: inject yields a [memo, whatever_each_yields] pair,
> and Hash#each yields a [key, value] pair. This is the analogous
> assignment:
>
> yielded_values = [:memo, [:key, :value]]
> memo, (key, value) = *yielded_values
>
> How would you say this in English? Inject is problematic this way; I
> don't understand how to explain it naturally using the word "inject" at
> all. But perhaps you could use a general form such as "call inject with
> the parameter initial_value and a block taking parameters memo, key and
> value."
>
>> Ex 2:
>> open('smiley.html', 'wb') do |f|
>> f << ...
>> end
>>
>> Sugestions ?.....
>
> *With* an open binary-write mode file handle to "smiley.html" *as* f...
>
> What a block means depends on the method. You've looked at iteration and
> scope-of-an-opened-object examples, which are probably the most common,
> but there are others (e.g. Markaby).
>
> Cheers,
> Dave


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

Dave Burt

10/9/2006 7:21:00 PM

0

Tuka Opaleye wrote:
> Thanks for the input guys. This really helps.

You're welcome. Glad to be of service.

> Sorry about the confusion with OR, I forgot that || actually means OR in
> most languages.

This one, too. "var ||= default" is a common Ruby idiom.

> I have heard |var| called goal-post notation (I think it was in a ROR
> podcast...) - is that what you call it ? I owuld guess another valid
> name is something with the word "pipes".

I haven't referred to them as goal-posts personally, but it's used
around the traps.

I'd just call them "block arguments" or "block parameters."

Why and his cat Trady Blix call the pipes surrounding the argument list
a chute. See the subheading Blocks in Why's (Poignant) Guide to Ruby,
Ch. 4, Sec. 4:

http://www.poignantguide.net/ruby/chapter-4.htm...

Cheers,
Dave

Max Muermann

10/9/2006 10:33:00 PM

0

On 10/10/06, Dave Burt <dave@burt.id.au> wrote:
> Tuka Opaleye wrote:
> > Thanks for the input guys. This really helps.
>
> You're welcome. Glad to be of service.
>
> > Sorry about the confusion with OR, I forgot that || actually means OR in
> > most languages.

Sorry about not understanding your question right...

Anyway, if it helps, I tend to think of the |var| notation as "with", e.g.

some_list.each do |item| ... end

becomes (at least in my head) some_list each do with item ...

Cheers,
Max

Tom Armitage

10/10/2006 9:10:00 AM

0

> Anyway, if it helps, I tend to think of the |var| notation as "with", e.g.
>
> some_list.each do |item| ... end
>
> becomes (at least in my head) some_list each do with item ...

Interesting - I tend to think of it as "as", which doesn't
transliterate as well, but indicates that the object inside the pipes
is temporary; it's just a name we're giving the iterated objects.

dblack

10/10/2006 9:15:00 AM

0

Yukihiro Matsumoto

10/10/2006 9:35:00 AM

0

Hi,

In message "Re: || explanation in ruby... in pseudolanguage"
on Tue, 10 Oct 2006 18:15:22 +0900, dblack@wobblini.net writes:

|Possibly...
|
| item = 1
| [2,3,4].each do |item| end
| p item # 4

Not in 1.9, where block parameters are local to the block, since
recent change.

matz.

Ara.T.Howard

10/10/2006 2:17:00 PM

0

Yukihiro Matsumoto

10/10/2006 2:26:00 PM

0

Hi,

In message "Re: || explanation in ruby... in pseudolanguage"
on Tue, 10 Oct 2006 23:16:53 +0900, ara.t.howard@noaa.gov writes:

|i know this has been discussed many times, and block locals definitely makes
|sense, but it __is__ a massive change that will break quite a bit of code.

Yes.

|is there somewhere the new scoping rules are spelled out so we can get a heads
|up?

Not yet. I am stuck with the idea named local variable propagation,
which upgrades variable scope if it is used outside of the scope (and
within a method), i.e.

def foo
4.times do
a = gets
end
p a # accessing a upgrades its scope.
end

But block parameters and explicitly declared block local variables are
not subject of this upgrading.

|is there a reccomended approach that will work in both 1.8 and 1.9 so as
|to smooth the transition?

Consider block parameters are block local even when you're using 1.8.
It's a good thing in general, I think.

matz.