[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: ruby / php operator differences.

Yukihiro Matsumoto

3/8/2007 7:30:00 AM

Hi,

In message "Re: ruby / php operator differences."
on Thu, 8 Mar 2007 16:11:52 +0900, Aaron Smith <beingthexemplary@gmail.com> writes:

|i wasn't 'claiming' anything. I was asking about why that was happening.
|I figured it out though.

Why that was happening? Could you tell me what you have figured out?

matz.

6 Answers

warhero

3/8/2007 3:37:00 PM

0

Hey Matz

I ended up writing the method a bit different,

int = @input_stream.read_special(1,'C')

if(int < 128)
STDOUT.puts "READ INT: #{int}"
return int
else
int = (int & 0x7f) << 7
tmp = @input_stream.read_special(1,'C')
if(tmp < 128)
STDOUT.puts "READ INT: #{int}"
return int | tmp
else
int = (int | (tmp & 0x7f)) << 7
tmp = @input_stream.read_special(1,'C')
if(tmp < 128)
STDOUT.puts "READ INT: #{int}"
return int | tmp
else
int = (int | (tmp & 0x7f)) << 8
tmp = @input_stream.read_special(1,'C')
int |= tmp

#Check if the integer should be negative
if ((int & 0x10000000) != 0)
## and extend the sign bit
int |= 0xe0000000
end
STDOUT.puts "READ INT: #{int}"
int
end
end
end


this seems to be working fine.

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

warhero

3/8/2007 10:37:00 PM

0

would it make sense that in php:
$int |= 0x10000000
is just shorthand for
$int = $int | 0x10000000

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

Brian Candler

3/9/2007 8:31:00 AM

0

On Fri, Mar 09, 2007 at 07:36:55AM +0900, Aaron Smith wrote:
> would it make sense that in php:
> $int |= 0x10000000
> is just shorthand for
> $int = $int | 0x10000000

It's the same in Ruby, except it goes further:

int |= 0x10000000

is short for

int = int | 0x10000000

is short for

int = int.|(0x10000000) # infix operator is really method call on LHS

is short for

int = int.send(:|, 0x10000000) # explicit method call by symbolic name

warhero

3/10/2007 4:55:00 AM

0

> It's the same in Ruby, except it goes further:
>
> int |= 0x10000000
>
> is short for
>
> int = int | 0x10000000
>
> is short for
>
> int = int.|(0x10000000) # infix operator is really method call on
> LHS
>
> is short for
>
> int = int.send(:|, 0x10000000) # explicit method call by symbolic name

pretty flippin cool.

in this example:
int = int.send(:|,0x1000000)

is it neccessary to have assignment portion? as in:
int.send(:|,0x1000000), does this take out the need for (int=)


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

Brian Candler

3/10/2007 3:01:00 PM

0

On Sat, Mar 10, 2007 at 01:55:19PM +0900, Aaron Smith wrote:
> > It's the same in Ruby, except it goes further:
> >
> > int |= 0x10000000
> >
> > is short for
> >
> > int = int | 0x10000000
> >
> > is short for
> >
> > int = int.|(0x10000000) # infix operator is really method call on
> > LHS
> >
> > is short for
> >
> > int = int.send(:|, 0x10000000) # explicit method call by symbolic name
>
> pretty flippin cool.
>
> in this example:
> int = int.send(:|,0x1000000)
>
> is it neccessary to have assignment portion? as in:
> int.send(:|,0x1000000), does this take out the need for (int=)

You don't need to make use of the return value. Every expression in Ruby
returns a value, but you can simply ignore it.

In your particular example, if you write

int = 4
int.send(:|, 0x10000000)

it's valid Ruby but isn't very useful, as it calculates a result and then
throws it away. But many methods do have side effects:

str = "hello"
str.send(:concat, " world")
puts str

Actually you'd normally write the middle line as

str.concat(" world")

or

str.concat " world"

or

str << " world"

(since the << infix operator expands to a << method call on str, and for
Strings the << method does the same as concat)

This stems from the fact that a String in Ruby is a mutable object (it can
change). Most objects are. However a few objects, in particular numbers and
symbols, are immutable. That is, you can't send a message to the number 5
telling it to change its value :-)

Regards,

Brian.

warhero

3/10/2007 3:58:00 PM

0

Brian Candler wrote:
> On Sat, Mar 10, 2007 at 01:55:19PM +0900, Aaron Smith wrote:
>> > int = int.|(0x10000000) # infix operator is really method call on
>>
>> is it neccessary to have assignment portion? as in:
>> int.send(:|,0x1000000), does this take out the need for (int=)

> .................

Ah, yes that makes sense. Thanks.

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