|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi people,
Did I dream about an opeartor |=? I can't find any reference to it in the online docs, yet I think I've seen it in some samples. What does it do? TIA -- Fernando Cacciola SciSoft http://fcacciola.50webs.com |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
> Did I dream about an opeartor |=?
> I can't find any reference to it in the online docs, yet I think I've > seen it in some samples. > What does it do? It sets a value if the variable does not have one, like: >> foo ||= :bar => :bar >> foo ||= :xyz => :bar Regards, -- Eustáquio "TaQ" Rangel http://eustaquiorangel.com "Simplicity is the ultimate sophistication." Leonardo da Vinci |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Nov 6, 2007, at 5:58 AM, Fernando Cacciola wrote: > Hi people, > > Did I dream about an opeartor |=? > > I can't find any reference to it in the online docs, yet I think > I've seen it in some samples. > > What does it do? > > TIA > It's not exactly an operator itself, it is syntactic sugar. You can find it well discussed in the archives. |= means David A. Black. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Fernando Cacciola wrote:
> Hi people, > > Did I dream about an opeartor |=? > > I can't find any reference to it in the online docs, yet I think I've > seen it in some samples. > > What does it do? I can see it in the pickaxe chm, under The Ruby Language => Expressions => Operator Expressions. I guess it's the same as leftval = leftval | rightval that is, binary OR. mortee |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
2007/11/6, Fernando Cacciola <fernando.cacciola@gmail.com>:
> Hi people, > > Did I dream about an opeartor |=? > It exists ![]() > > What does it do? > It works for integers like the binary OR: foo = 3 | 4 # result: 7 bar = 3 bar |= 4 # result: 7 Did you mean ||=? It is used to set default values if a variable is not definied yet. foo = 3 foo ||= 4 # foo doesn't change baz ||= 4 # baz is 4 if it wasn't defined before Regards, Thomas |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Paul Danese wrote:
> You might be thinking about ||= Ha, ya, that one... ![]() Thank you. > I believe a |= b "ORs" the bits in a and b and assigns them to a Oh, just like in C.... who would have thought (just kiding ![]() Best Fernando Cacciola SciSoft http://fcacciola.50webs.com |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Hi,
On Tue, 2007-11-06 at 21:21 +0900, Eustáquio 'TaQ' Rangel wrote: > > Did I dream about an opeartor |=? > It sets a value if the variable does not have one, like: > > >> foo ||= :bar > => :bar > >> foo ||= :xyz > => :bar Please take care not to confuse ||= with |=. ||= does as you say. |= is an assignment bitwise OR. Arlen |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
> operator |=
> Posted by Fernando Cacciola (Guest) on 06.11.2007 12:59 > > Hi people, > > Did I dream about an opeartor |=? > > I can't find any reference to it in the online docs, yet I think I've seen > it in some samples. > > What does it do? |= can also be used with arrays. shopping_list = ['peanut butter', 'grape jelly', 'whole wheat bread'] shopping_list |= ['peanut butter', 'sugar'] p shopping_list #=> ['peanut butter', 'grape jelly', 'whole wheat bread', 'sugar'] (cf. http://rubysnips.com/add-to-array-if ) Cheers, j. k. -- Posted via http://www.ruby-forum.com/. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On 11/6/07, Paul Danese <pdanese@rib-x.com> wrote:
> You might be thinking about ||= > ||= assigns a value to a variable if the variable is nil To be precise, it assigns a value to a variable which is either undefined, nil or false. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On 11/6/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
> |= means David A. Black. Hmmmm, I wonder if David knew that! <G> -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ |
|
![]() |
| Outils de la discussion | |
|
|