top | item 45626371

(no title)

ck45 | 4 months ago

But then Ruby only goes half way, not unlike the "watered-down form" in your term. Why is `#times` a method of Integer, but `#if` (or `#ifTrue`) not a method of booleans like in Smalltalk? Ruby does the same cherry picking from Smalltalk like everybody else, just different cherries. When looking at Ruby, it feels like the simple examples are all nice and clean but then the weird details start to appear and the language feels more hacky than others (like Ned Flander's house in Simpsons S08E08).

discuss

order

chao-|4 months ago

#if and #ifTrue are yours if you want them:

  class TrueClass
    def if = true
    def ifTrue = true
  end

  class FalseClass
    def if = false
    def ifTrue = false
  end

  true.if
  # => true
  false.if
  # => false

wild_egg|4 months ago

In Smalltalk those methods don't return `true`. They take a block and evaluate it if the boolean receiving the message

    (a > b) ifTrue: [ "do something" ]
EDIT: to clarify what's happening there, `>` is a message sent to `a` that will result in a boolean. The True class and False class both understand the ifTrue: message and `True>>ifTrue:` executes the block whereas `False>>ifTrue:` just throws it away.

There's no `if` keyword in the language. Control flow is done purely through polymorphism.

codesnik|4 months ago

basically it's because of "else" and "elsif". While ".each" works the same as "for .. in ...; end", it's harder to do "if else" as method which will also return value of the block inside the branch. Smalltalk can do it because "ifTrue:ifFalse:" is _one_ message, ruby didn't go that way syntactically.