Is there a way to....

Started by Seox, July 06, 2009, 10:25:31 am

Previous topic - Next topic

Seox

Do something like this?

kill_some_people unless moral_code == good
If so, then do ______.


instead of this:

kill_some_people unless moral_code == good
if moral_code == good....


That may not seem like much, but when the line goes ACROSS THE PAGE, it gets a bit ambiguous, repetitive, and also confusing as hell. I'm just looking for a way to go
DO A unless B, and if B, then do this instead.
without restating B. Ternary operators seem like they MIGHT work, but, for some reason, I don't seem to think they work for anything except assignment. Meh.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

unless X

is the same as
if not (X)


So this:
unless X
  # a
else
  # b
end

is the same as:
if not X
  # a
else
  # b
end

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Wow blizz that makes more sense...I even had troubles with the unless command. lol thnx

Seox

Let me restate,

$data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf] unless i_give_a_f***
if $data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf]



That's a bit long. It's not about if or unless, it's about, is there a way to avoid using up a WHOLE nother line, and making it look UBER confusing, just to restate the conditional?


Oh yeah. Unless/else. Der >.>. I'm just thinking about within the context of the statement modifiers.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

if you make it clear the lin is unfinished like leafing an && or || or ,  or (  or {   ect. at the end of the line then press enter and indent it will continue the lin on the next line.
i didn't really explain that will so I hope someone one else dose better or did i even answer your question?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Because indentation is used to mark where blocks start and end so the code gets readable.

@Seox: Why checking 2 times for the same condition? Use if-else/unless-else blocks.

And what you want is probably this.

if a then b
b if a
unless a then b
b unless a


I never tried the third one, BTW.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Fantasist

Quote
kill_some_people unless moral_code == good
If so, then do ______.


instead of this:

kill_some_people unless moral_code == good
if moral_code == good....



I'd do it this way, it's least confusing:


if moral_code == good
  #blah
else
  kill_some_people
end
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Seox

Thank you, everyone. I guess I'm just a lazy bastard who was looking to use statement modifiers like that, when I shoulda just stopped and thought.

^_^
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Fantasist

Not really. One fine day, you could realize s shortcut did exist and you haven't been using it.
I think you know this already, but large conditions can stored in local variables for convelience:
$data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf] unless i_give_a_f***
if $data_weapons[53].build_encryption_ray(743, ham, nine) + $data_weapons[oaphfoiwenbrkjbwmtgf]


can be:

condition = $data_weapons[53].build_encryption_ray(743, ham, nine) +
                $data_weapons[oaphfoiwenbrkjbwmtgf] unless i_give_a_f***
if condition


And don't forget the ternary operator ?: It's a handy replacement for small if-else statements, and it's my personal favorite.

Syntax:
condition ? statement_if_true : statement_if_false


For example, this:
moral_code == good ? donate : kill_someone


is the same as this:

if moral_code == good
  donate
else
  kill_someone
end
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




fugibo

If you really feel like using one line, you can encapsulate all the <evil> stuff and all the <good> stuff in their own methods, and have them return true or false depending on morals (in other words, the method "evil" would do evil things and return true if you are evil, and return false if not, and simiilar with the method "good"). Then you could just use

good unless evil


Of course, that's rather hard to read/impractical. But if you absolutely have to have one line...

Seox

O_O

Thank you, all of you.

Yeah, I LOVE ternary operators. They also sound cool as hell.

Thank you, LongFellow ^_^
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Fantasist

Ternary operators just mean they operate on 3 operands (binary 2, unary 1).
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Blizzard

Quote from: Longfellow on July 06, 2009, 08:22:38 pm
If you really feel like using one line, you can encapsulate all the <evil> stuff and all the <good> stuff in their own methods, and have them return true or false depending on morals (in other words, the method "evil" would do evil things and return true if you are evil, and return false if not, and simiilar with the method "good"). Then you could just use

good unless evil


Of course, that's rather hard to read/impractical. But if you absolutely have to have one line...


VERY impractical. "evil" would be executed every time.

Quote from: Seox on July 06, 2009, 09:05:47 pm
Yeah, I LOVE ternary operators. They also sound cool as hell.


I prefer "C ? A : B"  in short statements only.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

fugibo

I meant evil would be like

def evil
  if @morals == :wrong
    kill_people
    return true
  end
end


"evil" would then return nil (which acts like false when you use "if <var>") unless you are evil. It would execute, but it wouldn't do anything evil.

And lol, why didn't I think of ternary? I use it all the freaking time :P

Blizzard

July 07, 2009, 09:26:37 am #14 Last Edit: July 07, 2009, 09:28:57 am by Blizzard
That's a very inconsistent concept. -_- The cohersion of your methods is very bad.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

fugibo

Quote from: Blizzard on July 07, 2009, 09:26:37 am
That's a very inconsistent concept. -_- The cohersion of your methods is very bad.


That's why I called it impractical, lol. :P

Blizzard

And that's why I don't understand you going on with that idea. When I come to the conclusion that something is impractical, I stop thinking about it.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Seox

Quote from: Blizzard on July 07, 2009, 04:25:09 pm
And that's why I don't understand you going on with that idea. When I come to the conclusion that something is impractical, I stop thinking about it.


Hey Blizz. Breathing is impractical.

XD

Seriously though, at first, I really didn't think about "good coding" and "bad coding." You guys've really helped me to learn to NOT be a lazy-ass, both in and out of this thread. Thank you ^_^
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)