Forum: Ruby method proposition

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dirk M. (Guest)
on 2006-01-21 21:00
(Received via mailing list)
hi all,
what do you think of these methods:

class TrueClass
  def switch
    false
  end
end

class FalseClass
  def switch
    true
  end
end

i think i could find some real use for this, though switch! would
probably
be even more useful..
greetings, Dirk.
unknown (Guest)
on 2006-01-21 21:10
(Received via mailing list)
Hi --

On Sun, 22 Jan 2006, Dirk M. wrote:

>  def switch
>    true
>  end
> end
>
> i think i could find some real use for this, though switch! would probably
> be even more useful..

You don't like "not"? :-)

What would switch! do?


David

--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Dirk M. (Guest)
on 2006-01-21 21:19
(Received via mailing list)
hi,


> You don't like "not"? :-)

hmm.. guess i missed that ;-)


> What would switch! do?

switch! would change self

greetings, Dirk.
Dirk M. (Guest)
on 2006-01-21 21:22
(Received via mailing list)
hi again,

foobar=true
foobar=not(foobar)
doesn't seem to work, while i can see purpose in that,

light_switch=false
light_switch.switch if dark==true
Joe Van D. (Guest)
on 2006-01-21 21:22
(Received via mailing list)
On 1/21/06, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> wrote:
> >  end
>
> You don't like "not"? :-)
>
> What would switch! do?

True would become False.  False would become True...  Human sacrifice,
dogs and cats living together - mass hysteria!
unknown (Guest)
on 2006-01-21 21:25
(Received via mailing list)
Hi --

On Sun, 22 Jan 2006, Dirk M. wrote:

> switch! would change self
So:

   true.switch!
   1 == 1      # false

?


David

--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Joe Van D. (Guest)
on 2006-01-21 21:28
(Received via mailing list)
On 1/21/06, Dirk M. <removed_email_address@domain.invalid> wrote:
> switch! would change self
er, the global value 'true' is the only instance of TrueClass.  so,
doing true.switch! would make true become false?

So then,
if 1 == 1
  puts "hi"
end

Wouldn't print anything?

(I wouldn't want to maintain your programs  :-)
Dirk M. (Guest)
on 2006-01-21 21:34
(Received via mailing list)
> er, the global value 'true' is the only instance of TrueClass.  so,
> doing true.switch! would make true become false?
>
> So then,
> if 1 == 1
> puts "hi"
> end
>
> Wouldn't print anything?


that, on second thought, probably wouldn't work, and the following would
have to be used:

light_switch=false
light_switch=light_switch.switch if dark==true
Eero S. (Guest)
on 2006-01-21 21:43
(Received via mailing list)
On 2006.01.22 04:31, Dirk M. wrote:
>
> that, on second thought, probably wouldn't work, and the following would
> have to be used:
>
> light_switch=false
> light_switch=light_switch.switch if dark==true

I think here is where a Symbol would be most fitting ;)

  light_switch  = :off
  light_switch  = :on if dark?


E
Paul R. (Guest)
on 2006-01-21 21:53
(Received via mailing list)
On 21 Jan 2006, at 19:42, Eero S. wrote:

> I think here is where a Symbol would be most fitting ;)
>
>   light_switch  = :off
>   light_switch  = :on if dark?

I'd just like to point out that this now makes it 4 Ruby programmers
needed to change a lightbulb (or at least turn it on) so far in this
thread. Would anybody like to take a guess at how many it would have
taken in other languages?
Simon Kröger (Guest)
on 2006-01-21 22:15
(Received via mailing list)
Eero S. wrote:
>>>Wouldn't print anything?
>
>   light_switch  = :off
>   light_switch  = :on if dark?

I don't get it...

light_switch = dark

should be the same as

light_switch=false
light_switch=light_switch.switch if dark==true

right? (and *please* don't compare to 'true' if something is true, it
*is* already true, comparing it with true doesn't change that - never)

also

light_switch = !light_switch

should 'switch' the switch, right?

puzzled, or did i missed the joke?

Simon
unknown (Guest)
on 2006-01-21 22:46
(Received via mailing list)
Hi --

On Sun, 22 Jan 2006, Dirk M. wrote:

> hi again,
>
> foobar=true
> foobar=not(foobar)
> doesn't seem to work, while i can see purpose in that,

You would do:

x = (not x)

or

x = !x


David

--
David A. Black
removed_email_address@domain.invalid

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
Christian N. (Guest)
on 2006-01-22 00:37
(Received via mailing list)
removed_email_address@domain.invalid writes:

> You would do:
>
> x = (not x)

Is it just me, or does the precedence of "not" horribly suck? ;-)
This topic is locked and can not be replied to.