[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby gotchas

Mark Guzman

12/19/2006 10:32:00 PM

I've written up some ruby gotchas that have come up while using ruby.
I'm wondering if there are any other gotchas that I should be aware of.
The list is available at:
http://.../2006/12/14/ruby-gotchas-a...

I was considering adding &&= to the list as it's functionality seemed a
bit odd to me at first.
--mark

--
sic transit gloria et adulescentia
blog | http://...
wiki | http://wiki....


18 Answers

Peter Szinek

12/19/2006 11:09:00 PM

0

Mark Guzman wrote:
> I've written up some ruby gotchas that have come up while using ruby.
> I'm wondering if there are any other gotchas that I should be aware of.
> The list is available at:
> http://hasno.info/2006/12/14/ruby-gotchas-a...
>
> I was considering adding &&= to the list as it's functionality seemed a
> bit odd to me at first.
> --mark
>
From the 'Ruby way':

foo = false
bar = true

baz = foo or bar

baz ends up false (because = has greater priority than or)

Cheers,
Peter

__
http://www.rubyra...

ramalho@gmail.com

12/20/2006 2:59:00 AM

0

On 12/19/06, Peter Szinek <peter@rubyrailways.com> wrote:
> foo = false
> bar = true
>
> baz = foo or bar
>
> baz ends up false (because = has greater priority than or)

That sucks. Why would anyone want = to be evaluated before anything else?

I want to be enlightened.

Cheers,

Luciano

Tom Pollard

12/20/2006 3:54:00 AM

0


On Dec 19, 2006, at 9:58 PM, Luciano Ramalho wrote:

> On 12/19/06, Peter Szinek <peter@rubyrailways.com> wrote:
>> foo = false
>> bar = true
>>
>> baz = foo or bar
>>
>> baz ends up false (because = has greater priority than or)
>
> That sucks. Why would anyone want = to be evaluated before anything
> else?

The point is that 'or' and 'and' have /lower/ precedence than
anything else, so that they can be used to chain complete expressions
together. This is a Perl-ism, to my knowledge and a number of
common Perl idioms are based on this. (...like, 'open(my $handle,
$filename) or die'). I'm a nuby here, but I haven't seen the weak
'and' and 'or' used much in Ruby - at least, not in the same
circumstances where I'm used to seeing it in Perl code.

Cheers,

TomP


dblack

12/20/2006 4:01:00 AM

0

Peter Szinek

12/20/2006 8:47:00 AM

0

Luciano Ramalho wrote:
> On 12/19/06, Peter Szinek <peter@rubyrailways.com> wrote:
>> foo = false
>> bar = true
>>
>> baz = foo or bar
>>
>> baz ends up false (because = has greater priority than or)
>
> That sucks.
You can't generalize to everybody just because it sucks for you :)

Why would anyone want = to be evaluated before anything else?
The question is not why would anyone want this (btw why not? btw2 this
is exactly what happened here: = was evaluated before or) - but rather
'why it is more logical for someone that the rvalue is computed first
then passed to the lvalue'. Hmm, maybe because it is so in the languages
from where the OP comes from? (At least Java, just tested... i.e. this code:

boolean y = false;
boolean z = true;
boolean x = y || z;
System.err.println(x);

prints 'true'

(I did not write a single line of C# code yet but I guess it's the same
there).

Cheers,
Peter

__
http://www.rubyra...


ramalho@gmail.com

12/20/2006 1:51:00 PM

0

Thanks, Tom. This is exactly the sort of explanation I was looking for.

Folks, I was not trying to bash ruby, just trying to figure out why
something that looked very strange to me is the way it is.

Cheers,

Luciano

On 12/20/06, Tom Pollard <tomp@earthlink.net> wrote:
> The point is that 'or' and 'and' have /lower/ precedence than
> anything else, so that they can be used to chain complete expressions
> together. This is a Perl-ism, to my knowledge and a number of
> common Perl idioms are based on this. (...like, 'open(my $handle,
> $filename) or die').

Peter Szinek

12/20/2006 2:14:00 PM

0

Luciano Ramalho wrote:
> Thanks, Tom. This is exactly the sort of explanation I was looking for.
>
> Folks, I was not trying to bash ruby, just trying to figure out why
> something that looked very strange to me is the way it is.

Sorry Lucio, I have misinterpreted your mail (I have thought you are
actually arguing that Ruby's handling of 'or' vs '=' is the natural way)
and after re-reading I realized that you were upset about quite the
opposite thing...

Cheers,
Peter

__
http://www.rubyra...


Brad Phelan

12/20/2006 3:54:00 PM

0

Mark Guzman wrote:
> I've written up some ruby gotchas that have come up while using ruby.
> I'm wondering if there are any other gotchas that I should be aware of.
> The list is available at:
> http://hasno.info/2006/12/14/ruby-gotchas-a...
>
> I was considering adding &&= to the list as it's functionality seemed a
> bit odd to me at first.
> --mark
>

The follow gets on my nerves a bit

class A
def foo
yield "foo"
end
end

a = A.new
a.foo do |a|
puts a
end

a.foo do |a|
puts a
end

---

foo
test.rb:12: undefined method `foo' for "foo":String (NoMethodError)

---

Arguments to blocks are not scoped locally if the
name already exists in the outer scope. I think this
might be changing in Ruby 2.0???

--
Brad Phelan
http://xt...

dblack

12/20/2006 5:02:00 PM

0

Giles Bowkett

12/20/2006 7:28:00 PM

0

> > foo = false
> > bar = true
> >
> > baz = foo or bar
> >
> > baz ends up false (because = has greater priority than or)
>
> That sucks. Why would anyone want = to be evaluated before anything else?

Dude, the creator of the language reads this list. That doesn't suck,
you just don't understand it. Have some manners.

> I want to be enlightened.

I want a pet monkey. Does that obligate you to provide me with one?

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...