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.
A16652fd5d83c0473bd1e39d9a2117a6?d=identicon&s=25 Dirk Meijer (Guest)
on 2006-01-21 20: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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-21 20:10
(Received via mailing list)
Hi --

On Sun, 22 Jan 2006, Dirk Meijer 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
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
A16652fd5d83c0473bd1e39d9a2117a6?d=identicon&s=25 Dirk Meijer (Guest)
on 2006-01-21 20: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.
A16652fd5d83c0473bd1e39d9a2117a6?d=identicon&s=25 Dirk Meijer (Guest)
on 2006-01-21 20: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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 20:22
(Received via mailing list)
On 1/21/06, dblack@wobblini.net <dblack@wobblini.net> 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!
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-21 20:25
(Received via mailing list)
Hi --

On Sun, 22 Jan 2006, Dirk Meijer wrote:

> switch! would change self
So:

   true.switch!
   1 == 1      # false

?


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 20:28
(Received via mailing list)
On 1/21/06, Dirk Meijer <hawkman.gelooft@gmail.com> 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  :-)
A16652fd5d83c0473bd1e39d9a2117a6?d=identicon&s=25 Dirk Meijer (Guest)
on 2006-01-21 20: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
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2006-01-21 20:43
(Received via mailing list)
On 2006.01.22 04:31, Dirk Meijer 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
66247570dbe0c350a165cdabc2769ba1?d=identicon&s=25 Paul Robinson (Guest)
on 2006-01-21 20:53
(Received via mailing list)
On 21 Jan 2006, at 19:42, Eero Saynatkari 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?
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 Simon Kröger (Guest)
on 2006-01-21 21:15
(Received via mailing list)
Eero Saynatkari 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-21 21:46
(Received via mailing list)
Hi --

On Sun, 22 Jan 2006, Dirk Meijer 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
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-21 23:37
(Received via mailing list)
dblack@wobblini.net 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.