[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Defining <<

Luke Ivers

2/7/2007 9:47:00 PM

I'm not 100% how exactly to search to find out if someone else has posed
this question, but why does the following happen?

Given:

class Hash
def << (key, val=nil)
self.store(key, val)
end
end

h = {}
h << 'test'
h << 'test', 'bob'

Gives back:

test.rb:9: parse error, unexpected ',', expecting $
h << 'test', 'bob'

If I change the last line to:

h << ('test', 'bob')

I get back:

test.rb:9: parse error, unexpected ',', expecting ')'
h << ('test', 'bob')

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

18 Answers

Keynan Pratt

2/7/2007 9:52:00 PM

0

on occasion this space
def << (key, val=nil)

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

Keynan Pratt

2/7/2007 9:52:00 PM

0

on occasion this space
def << (key, val=nil)
^

causes problems

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

Gary Wright

2/7/2007 9:59:00 PM

0


On Feb 7, 2007, at 4:46 PM, Luke Ivers wrote:

> I'm not 100% how exactly to search to find out if someone else has
> posed
> this question, but why does the following happen?
>
> Given:
>
> class Hash
> def << (key, val=nil)
> self.store(key, val)
> end
> end
>
> h = {}
> h << 'test'
> h << 'test', 'bob'

The syntax rules for the << operator don't allow it to take multiple
arguments when called via infix notation:

h << arg1 # one argument only

you can call the method with multiple arguments but you've got to do
it like:

h.<<(arg1, arg2) # dot-style method invocation

You can use an array to 'cheat':

h << [arg1, arg2]

But the method will only see one argument, an array, and you would have
to expect that and/or test for it in your definition for Hash#<<.

Gary Wright




Robert Klemme

2/7/2007 10:05:00 PM

0

On 07.02.2007 22:58, Gary Wright wrote:
>
> On Feb 7, 2007, at 4:46 PM, Luke Ivers wrote:
>
>> I'm not 100% how exactly to search to find out if someone else has posed
>> this question, but why does the following happen?
>>
>> Given:
>>
>> class Hash
>> def << (key, val=nil)
>> self.store(key, val)
>> end
>> end
>>
>> h = {}
>> h << 'test'
>> h << 'test', 'bob'
>
> The syntax rules for the << operator don't allow it to take multiple
> arguments when called via infix notation:
>
> h << arg1 # one argument only
>
> you can call the method with multiple arguments but you've got to do it
> like:
>
> h.<<(arg1, arg2) # dot-style method invocation
>
> You can use an array to 'cheat':
>
> h << [arg1, arg2]
>
> But the method will only see one argument, an array, and you would have
> to expect that and/or test for it in your definition for Hash#<<.

Here's another variant:

class Hash
Proxy = Struct.new :parent, :key do
def <<(val)
parent[key] = val
parent
end
end
def <<(key)
Proxy.new self, key
end
end

irb(main):012:0> h ={}
=> {}
irb(main):013:0> h << "foo" << "bar" << "key" << 234
=> {"foo"=>"bar", "key"=>234}
irb(main):014:0> h << 2 << 3
=> {2=>3, "foo"=>"bar", "key"=>234}
irb(main):015:0> h
=> {2=>3, "foo"=>"bar", "key"=>234}

Not that I would recommend it...

Kind regards

robert

Simon Kröger

2/8/2007 9:03:00 AM

0

Robert Klemme wrote:

> [...]
> irb(main):013:0> h << "foo" << "bar" << "key" << 234
> [...]
>
> Not that I would recommend it...

Yeah, i like

h <= "foo" > "bar" <= "key" > 42

:)

unfortunately there is no => operator...

cheers

Simon

BIG Bird

12/8/2012 1:24:00 PM

0


"PJ O'D" <gapaddy@gmail.com> wrote in message
news:3367e376-82e7-44c5-8223-01a5a1e77644@b11g2000yqh.googlegroups.com...
On Dec 8, 7:10 am, Earl Evleth <evl...@wanadoo.fr> wrote:

> > Suggestion: If an American is on welfare and hasn't paid a
> > predetermined amount into either SS or FIT for that year they should
> > forfeit their right to vote for that year.
.
> Stupid.
>
> I note that an IQ condition was applied ,,,

Obama would never have won either election.

2012 Presidential election results from CNN
Vote by Education

No high school:
64% Obama
35% Romney

College graduate
47% Obama
51% Romney




the only ones who should be prevented from voting are you trailer trash, sister
raping, nascar watching,
inbred aberrations of evolution and fugitives from the laws of nature


hell, you dick sucking crackers,hillbillies,and rednecks should be strung up on
the nearest tree or lamp post,
and that includes,men,women,children, AND babies, and left to rot until your bones
bleach

America and Americans can not, and will not Tolerate Hillbilly LOOOOOOSERS!!!! in
their midst


Runge 667

12/8/2012 1:42:00 PM

0

How can americans bear old kooks like donovan ??!!
Theu already sent us evleth, duh.

"BIG BIRD" <Snordo@Teranews.com> a ?crit dans le message de groupe de
discussion : WbHws.1271$3S5.1006@newsfe18.iad...
>
> "PJ O'D" <gapaddy@gmail.com> wrote in message
> news:3367e376-82e7-44c5-8223-01a5a1e77644@b11g2000yqh.googlegroups.com...
> On Dec 8, 7:10 am, Earl Evleth <evl...@wanadoo.fr> wrote:
>
>> > Suggestion: If an American is on welfare and hasn't paid a
>> > predetermined amount into either SS or FIT for that year they should
>> > forfeit their right to vote for that year.
> .
>> Stupid.
>>
>> I note that an IQ condition was applied ,,,
>
> Obama would never have won either election.
>
> 2012 Presidential election results from CNN
> Vote by Education
>
> No high school:
> 64% Obama
> 35% Romney
>
> College graduate
> 47% Obama
> 51% Romney
>
>
>
>
> the only ones who should be prevented from voting are you trailer trash,
> sister
> raping, nascar watching,
> inbred aberrations of evolution and fugitives from the laws of nature
>
>
> hell, you dick sucking crackers,hillbillies,and rednecks should be strung
> up on
> the nearest tree or lamp post,
> and that includes,men,women,children, AND babies, and left to rot until
> your bones
> bleach
>
> America and Americans can not, and will not Tolerate Hillbilly
> LOOOOOOSERS!!!! in
> their midst
>
>

simon calder

12/8/2012 2:14:00 PM

0

X-No-Archive:Yes


"Earl Evleth" <evleth@wanadoo.fr> wrote in message
news:CCE8ECB3.23365D%evleth@wanadoo.fr...
> On 8/12/12 12:05, in article
> cfb1b024-8557-4bbc-911f-20b06771f044@a2g2000yqh.googlegroups.com, "PJ O'D"
> <zuckerlass@yahoo.com> wrote:
>
>> Suggestion: If an American is on welfare and hasn't paid a
>> predetermined amount into either SS or FIT for that year they should
>> forfeit their right to vote for that year.
>
>
> Stupid. This would never become law, with universal sufferage
> there is no such condition attached.

Don't stop these bigots from suggesting it though...!
Whatever disability has to do with voting.....I'm at a loss to grasp. Ditto
unemployed...


clem



simon calder

12/8/2012 2:15:00 PM

0

X-No-Archive:Yes


"PJ O'D" <gapaddy@gmail.com> wrote in message
news:3367e376-82e7-44c5-8223-01a5a1e77644@b11g2000yqh.googlegroups.com...
On Dec 8, 7:10 am, Earl Evleth <evl...@wanadoo.fr> wrote:

> > Suggestion: If an American is on welfare and hasn't paid a
> > predetermined amount into either SS or FIT for that year they should
> > forfeit their right to vote for that year.
.
> Stupid.
>
> I note that an IQ condition was applied ,,,

Obama would never have won either election.

2012 Presidential election results from CNN
Vote by Education

No high school:
64% Obama
35% Romney

College graduate
47% Obama
51% Romney
**********************************

Ha, ha..... QED !!!!!

You're not bright enough to understand that IQ is not measured by High
School OR College attendance.....
Oh dear..


clem




simon calder

12/8/2012 2:17:00 PM

0

X-No-Archive:Yes


"BIG BIRD" <Snordo@Teranews.com> wrote in message
news:WbHws.1271$3S5.1006@newsfe18.iad...
>
> "PJ O'D" <gapaddy@gmail.com> wrote in message

> the only ones who should be prevented from voting are you trailer trash,
> sister
> raping, nascar watching,
> inbred aberrations of evolution and fugitives from the laws of nature

Is any of this factually correct for POD....?
I'm fascinated to learn...


clem