[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Conventions in ruby and the principle of least surprise

Einar Magnús Boson

1/27/2009 2:12:00 AM

Hi guys, and hi Matz. I have a few questions about ruby syntax and
library that so far has had me surprised. I know Matz doesn't say that
ruby shouldn't surprise me, but rather not surprise him but still, i
would like to hear the reasoning behind a few weird things:

do..end vs. {}

Say I have this function:

def save_block name, &b
@blocks ||= {}
@blocks[name] = b
end

Now this is fine:

save_block :say_hello do
puts "hello!"
end

but not this:

save_block :say_hello {
puts "hello!"
}

I find that rather annoying but I guess it's to be able to write things
like

save_block ["name1_long", "name2_long", "short"].find {|n| n.size <=
5}.to_sym do
puts "hello!"
end

Personally I would rather have had to use parentheses to bind the block
to find, but I realize that it's hardly up to me :)

Anyways. More serious, in my oppinion, is the fact that Array#insert and
String#insert are destructive without having an exclamation mark. Why is
that? I really do not expect them to change their owner.

>> hi = "hello world"
=> "hello world"
>> puts hi
hello world
=> nil
>> puts hi.insert(6, "surprising ")
hello surprising world
=> nil
>> puts hi
hello surprising world
=> nil

seriously, wtf? Why not `insert` and `insert!` that behave as expected?
Before I realized this I had a hard time tracking down some unexpected
behaviour.
--
Posted via http://www.ruby-....

9 Answers

Brian Candler

1/27/2009 8:26:00 AM

0

Einar Boson wrote:
> Now this is fine:
>
> save_block :say_hello do
> puts "hello!"
> end
>
> but not this:
>
> save_block :say_hello {
> puts "hello!"
> }

Although this is fine:

save_block(:say_hello) {
puts "hello!"
}
--
Posted via http://www.ruby-....

Damjan Rems

1/27/2009 10:08:00 AM

0


My convention is use {} in oneliners. In all other examples I use
do..end.

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

Ryan Davis

1/27/2009 9:12:00 PM

0


On Jan 27, 2009, at 02:08 , Damjan Rems wrote:

> My convention is use {} in oneliners. In all other examples I use
> do..end.

I follow (and teach) the weirich method:

http://onestepback.org/index.cgi/Tech/Ruby/BraceVs...

{} for block values I care about (think map), do/end for regular block
sends (think each).

I use {} for blocks that return values (think #map) and do/end for all
other blocks (think #each).


Mike Gold

1/28/2009 12:22:00 AM

0

Ryan Davis wrote:
> On Jan 27, 2009, at 02:08 , Damjan Rems wrote:
>
>> My convention is use {} in oneliners. In all other examples I use
>> do..end.
>
> I follow (and teach) the weirich method:
>
> http://onestepback.org/index.cgi/Tech/Ruby/BraceVs...
>
> {} for block values I care about (think map), do/end for regular block
> sends (think each).
>
> I use {} for blocks that return values (think #map) and do/end for all
> other blocks (think #each).

funcs = []
(1..5).each do |i|
funcs << lambda { i }
end
p funcs.map { |f| f.call } # => [1, 2, 3, 4, 5]

funcs2 = []
for j in 1..5
funcs2 << lambda { j }
end
p funcs2.map { |f| f.call } # => [5, 5, 5, 5, 5]

I prefer to make the new binding scope of a block visually obvious,
therefore I always use {}. do/end tricks me into thinking it belongs to
the same category as for/end or while/end or if/end, but it's quite
different (above).

Also, I like one rule better than two rules.
--
Posted via http://www.ruby-....

Phlip

1/28/2009 11:35:00 PM

0

Mike Gold wrote:

> funcs2 = []
> for j in 1..5
> funcs2 << lambda { j }
> end
> p funcs2.map { |f| f.call } # => [5, 5, 5, 5, 5]
>
> I prefer to make the new binding scope of a block visually obvious,
> therefore I always use {}. do/end tricks me into thinking it belongs to
> the same category as for/end or while/end or if/end, but it's quite
> different (above).
>
> Also, I like one rule better than two rules.

Curiously, Ruby departs with 40 years of Structural Programming tradition - the
statement groups controlled by if-end, and their ilk, do _not_ introduce a new
variable scope, while the statement groups inside true blocks _do_ introduce
scoped variables.

Furtherless, the region inside a block _might_ be someone else's scope! Rails's
render :update do ... end does this to us. Principle of Most Surprise applies.

So making dangerous things like blocks look ugly is a good way to help us
respect them!

--
Phlip

Rick DeNatale

2/2/2009 4:45:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Tue, Jan 27, 2009 at 4:12 PM, Ryan Davis <ryand-ruby@zenspider.com>wrote:

>
> On Jan 27, 2009, at 02:08 , Damjan Rems wrote:
>
> My convention is use {} in oneliners. In all other examples I use
>> do..end.
>>
>
> I follow (and teach) the weirich method:
>
> http://onestepback.org/index.cgi/Tech/Ruby/BraceVs...
>
> {} for block values I care about (think map), do/end for regular block
> sends (think each).
>
> I use {} for blocks that return values (think #map) and do/end for all
> other blocks (think #each).
>

Yes, it's the "I care about' that matters, a subtle distinction to the
weirich method, which I wrote about:

http://talklikeaduck.denh...articles/2007/10/02/ruby-blocks-d...

and Jim agreed in the comments.

--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...

suzeeq

7/4/2014 8:17:00 PM

0

The Starmaker wrote:
> Joan in GB-W wrote:
>> Excuse me for asking a PC question rather than a TV one.
>> Multiple times a day a pop-up appears on my PC screen saying that JAVA is
>> Recommended. Should I continue to ignore this?
>
>
> use your right mouse button
> on Java button
> click Properties
> then
> uncheck updates automatically
>
>
> see if that works.

I can't. No 'Java button' or choice comes up when I click the R mouse
button.

Adam H. Kerman

7/4/2014 8:39:00 PM

0

suzeeq <suzee@imbris.com> wrote:
>The Starmaker wrote:
>>Joan in GB-W wrote:

>>>Excuse me for asking a PC question rather than a TV one.
>>>Multiple times a day a pop-up appears on my PC screen saying that JAVA is
>>>Recommended. Should I continue to ignore this?

>>use your right mouse button
>>on Java button
>>click Properties
>>then
>>uncheck updates automatically

>>see if that works.

>I can't. No 'Java button' or choice comes up when I click the R mouse
>button.

Then go through Control Panel to find the java control panel. Do you
know for certain that you installed java? Check "add and remove programs"
to see if it's there.

suzeeq

7/4/2014 8:44:00 PM

0

Adam H. Kerman wrote:
> suzeeq <suzee@imbris.com> wrote:
>> The Starmaker wrote:
>>> Joan in GB-W wrote:
>
>>>> Excuse me for asking a PC question rather than a TV one.
>>>> Multiple times a day a pop-up appears on my PC screen saying that JAVA is
>>>> Recommended. Should I continue to ignore this?
>
>>> use your right mouse button
>>> on Java button
>>> click Properties
>>> then
>>> uncheck updates automatically
>
>>> see if that works.
>
>> I can't. No 'Java button' or choice comes up when I click the R mouse
>> button.
>
> Then go through Control Panel to find the java control panel. Do you
> know for certain that you installed java? Check "add and remove programs"
> to see if it's there.

CP on the browser or Windows? I think Java's in there. Not that it matters.