News for hackers. Stuff that matters.
Re:Ruby 3.2.0 Is from Another Dimension by capableweb on Wednesday January 04, 2023 @06:14AM (#34244294)
Django might be better but Ruby as a language is way better thought out than Python. Python feels like it's cobbled together compared to Ruby. There is a certain elegance with the language, that is hard to find elsewhere.
Re:Ruby 3.2.0 Is from Another Dimension by empthought on Wednesday January 04, 2023 @07:22AM (#34244715)
I disagree. I perceive no difference in elegance between the two languages nor a particular “cobbled together” aspect to Python.
Re:Ruby 3.2.0 Is from Another Dimension by scotty79 on Wednesday January 04, 2023 @07:45AM (#34244866)
Meta classes, dict/slot are some really inelegant stuff.
Re:Ruby 3.2.0 Is from Another Dimension by empthought on Wednesday January 04, 2023 @08:12AM (#34245117)
I disagree. They are elegant.
Re:Ruby 3.2.0 Is from Another Dimension by arcturus17 on Wednesday January 04, 2023 @09:24AM (#34245791)
I use Python every day and I like it. However, functional semantics are not elegant at all IMO. Using list() and map() all over the place is not cool, and like many others say, list comprehensions, though concise, are not always that readable (and I say this as someone who reads and writes plenty of them). Lambdas are not great, either.

In Django in particular, async feels like an afterthought, compared to other frameworks where it's first class.

Re:Ruby 3.2.0 Is from Another Dimension by empthought on Wednesday January 04, 2023 @11:44AM (#34247867)
Python lambdas are not great but Ruby not having first-class functions is also not great. Neither are perfect and there is no syntactic reason outside of variations of personal taste to choose one language over the other.

There are a boatload of compelling reasons to choose Python for its libraries and documentation and adoption level, though.

Re:Ruby 3.2.0 Is from Another Dimension by arcturus17 on Wednesday January 04, 2023 @11:53AM (#34248004)
I didn't know Ruby didn't have first-class functions but that's already a massive no for me. You don't need to convince me regarding Python though - it's too convenient to pass up. And despite its shortcomings it's certainly not a language devoid of elegance like some in this thread are painting... Like the old adage goes, there's the languages nobody uses, and there's the languages people talk shit about. Or in simpler terms, as seen in this thread: "everything I don't use is utter shit".
Re:Ruby 3.2.0 Is from Another Dimension by djur on Wednesday January 04, 2023 @03:43PM (#34251227)
I have no idea what they mean by Ruby not having first-class functions. Of course it does. It's a fundamental feature of the language.
Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @01:20PM (#34264097)
No it does not. Ruby has first-class procs. They are not the same thing. It's a fundamental flaw of the language. It's in the Wikipedia page.

https://en.wikipedia.org/wiki/First-class_function

"The identifier of a regular "function" in Ruby (which is really a method) cannot be used as a value or passed. It must first be retrieved into a Method or Proc object to be used as first-class data. The syntax for calling such a function object differs from calling regular methods."

Why do people like you insist on talking about things you don't actually know about?

Re:Ruby 3.2.0 Is from Another Dimension by djur on Thursday January 05, 2023 @01:34PM (#34264307)
The Wikipedia article is wrong. A proc is a function. The misunderstanding here is confusing methods with functions. "def foo" defines a method, not a function. That same article claims that Java has first class functions on the basis of anonymous inner classes, that C# has them because of lambda expressions, etc. OCaml has different syntax for functions and methods. Does that mean it doesn't have first-class functions?

You should really consider whether this is an important enough topic to be worth personally insulting people over.

Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @02:29PM (#34265184)
Ruby doesn't have first-class methods, either. Python has both first-class methods and first-class functions.

> In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.[1]

OCaml has first-class functions, the names can be passed around as parameters and returned as values. Ruby functions and methods cannot; only Proc objects and Method objects proxying for them can be.

You should really consider whether pointing out your incorrectness constitutes an insult and why you think this is so.

Re:Ruby 3.2.0 Is from Another Dimension by djur on Thursday January 05, 2023 @03:02PM (#34265688)
Everything is an object in Ruby. Proc objects and Method objects _are_ functions. The same is true in Python -- functions are instances of the 'function' class. What's the difference between a proc and a function? Nothing.

"[P]eople like you insist on talking about things you don't actually know about" is insulting language. Consider discussing this with some people you know in real life to see what they think.

Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @03:39PM (#34266201)
You can’t use a Proc or Method value directly to call a method with the Ruby method invocation syntax! In Python and JavaScript and Scheme and OCaml you can! This is the whole idea behind “first-class.” Whether “everything is an object” or not is irrelevant.
Re:Ruby 3.2.0 Is from Another Dimension by djur on Thursday January 05, 2023 @03:59PM (#34266462)
There is nothing about first-class functions that requires function call syntax to be the same as method invocation syntax. OCaml, Objective-C, and Smalltalk all have different syntax for method invocation and function calls.

To make it concrete: what are you unable to do in Ruby that you're able to do in the languages that you claim do have first-class functions?

Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @10:15PM (#34270501)
This is such an asinine argument. Function pointers and passing an environment struct in C let you do everything an OO language does. What are you unable to do in C that you can do in Ruby?

You can implement a type system in Scheme if you really want to, what can you do in Haskell that you can’t do in Scheme?

Just admit you are wrong, bud. It’s not as hard as you’re making it out to be. Strachey’s definition is quite clear and Ruby doesn’t have first-class functions nor first-class methods. It’s a language flaw just like Python’s lambda is a language flaw.

Re:Ruby 3.2.0 Is from Another Dimension by djur on Thursday January 05, 2023 @11:35PM (#34271005)
I'm not sure how clear Strachey's definition can be, considering he never provided one. But his description of ALGOL procedures as second-class mentions that they can "never be represented by a variable or expression":

https://en.wikipedia.org/wiki/First-class_citizen#History

In Ruby you can assign a function to a variable, pass it to a function as a parameter, return it as the result of a function, and test two function objects for equality. Ruby has first-class functions. If it didn't, you would easily be able to provide an example of a program using first-class functions that cannot be represented in Ruby.

Re:Ruby 3.2.0 Is from Another Dimension by dragonwriter on Thursday January 05, 2023 @03:07PM (#34265754)
> Ruby has first-class procs.

Procs are functions (at least, they are functionally equivalent to Python functions—as pure OO languages, in both Ruby and Python first-class callable entities are richer than classic functions, even when counting closures as a kind functions and not a separate thing that includes a function, because they can carry metadata besides the environment of a closure.)

> It's in the Wikipedia page.

The Wikipedia page is wrong in that conclusion.

> The identifier of a regular "function" in Ruby (which is really a method) cannot be used as a value or passed

A method is a very different thing than a function. But, on a deeper level, I think it is also a mistake to call the thing it is referring the identifier for the method, its just syntax sugar for a symbol passed to a #send call, which, if send isn't overwritten on the sender (which it probably shouldn’t be, overriding send is a good way to cause chaos) will result in the method being called, unless some other method has priority.

> The syntax for calling such a function object differs from calling regular methods

The syntax is different only inna waybthat reflects thebdifferent behavior. Calling #call on a Proc or Method object (again, assuming a sane world where the object’s send method isn’t overidden) does dynamic method lookup on the :call name in the Method or Proc, and its syntax is exactly normal method-call syntax for that behavior (unless you use the shortcut syntax of “.()” for “.call()”, but that's just syntax sugar).

Using normal method call syntax does dynamic lookup on the method name provided in the call on the receiver object identified in the call.

That is, Ruby methods are first class, but what it is a convenient shortcut to think of as “method call syntax” in Ruby is not really method call syntax, it is syntax for a message send that will (by overridable default) result in calling a method with that name if it exists in the ancestor chain of classes for the object, so it is inaccurate to view the “method call” syntax excluding any parameters as “the identifier for the method”.

Re:Ruby 3.2.0 Is from Another Dimension by djur on Thursday January 05, 2023 @04:10PM (#34266642)
> its just syntax sugar for a symbol passed to a #send call

This isn't the case. If you override `send` it only affects direct usage of `send`. You can't override method dispatch. You can in some other OO languages but not Ruby.

Agreed otherwise. `x.foo` in Python and `x.method(:foo)` in Ruby do the exact same thing -- return a bound method object that can be called. These are both "proxies" in a sense but they're also first-class functions in all meaningful senses.

Re:Ruby 3.2.0 Is from Another Dimension by dragonwriter on Thursday January 05, 2023 @05:16PM (#34267648)
> This isn't the case. If you override `send` it only affects direct usage of `send`.

Correct, I both (minor) confused #send with #__send__ and misremebered the mechanism of some deep metaprogramming hacks involving a a mostly blank proxy object and #method_missing as using metaclasses and #send/#__send__ hacking; where that came from I don’t know. Should have not mentioned overriding (as its a tangent, in any case, as well as vmbeung wrong) or the #send method and referred to the fundamental underlying message send operation.

Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @10:25PM (#34270583)
You just gave away the difference right there. x.foo doesn’t “return a bound method object.” It just accesses an attribute and returns the value of that attribute. The attribute can be any Python value. If it’s a callable then it can be called with parens. There is no proxying whatsoever.

In contrast x.method(:foo) does do that proxy construction that you describe… because Ruby does not have first-class functions.

Re:Ruby 3.2.0 Is from Another Dimension by djur on Thursday January 05, 2023 @11:24PM (#34270940)
> x.foo doesn’t “return a bound method object.” It just accesses an attribute and returns the value of that attribute. The attribute can be any Python value.

Python docs, section 3.2, subsection "Callable types".

https://docs.python.org/3/reference/datamodel.html#the-stand...

x.foo returns an "instance method object", which is a "callable type". Also referred to as a "bound method", the same term that Ruby and JavaScript use.

> When an instance method object is called, the underlying function (__func__) is called, inserting the class instance (__self__) in front of the argument list. For instance, when C is a class which contains a definition for a function f(), and x is an instance of C, calling x.f(1) is equivalent to calling C.f(x, 1)... Note that the transformation from function object to instance method object happens each time the attribute is retrieved from the instance... It is also important to note that user-defined functions which are attributes of a class instance are not converted to bound methods; this only happens when the function is an attribute of the class.

This last part is why the following program has an error:

   def foo(self):
       print(self)

   class Foo:
       def __init__(self, fn):
           self.foo = fn

   x = Foo(foo)
   x.foo()
Re:Ruby 3.2.0 Is from Another Dimension by empthought on Friday January 06, 2023 @01:42PM (#34278976)
You are studiously missing the point and spamming irrelevant details. Yes, there's a Python "proxy" in your docs but it is syntactically transparent. Proc's proxy is not transparent.

me:

> If it's a callable then it can be called with parens

The docs:

> These are the types to which the function call operation (see section Calls) can be applied:

True of Python instance method objects. False of Ruby Methods and Procs. Therefore Python has first-class methods, and Ruby does not have first-class functions nor first-class methods. End of discussion.

Re:Ruby 3.2.0 Is from Another Dimension by djur on Friday January 06, 2023 @07:00PM (#34283197)
In Ruby, the function call operator, `.()`, can be used on both Method and Proc objects. This being different from the syntax used for method invocation does not have anything whatsoever to do with whether functions are first class. You are both wrong and being unreasonably rude about it.
Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @10:29PM (#34270612)
tl; dr it’s true, Ruby does not have first-class functions nor first-class methods…
Re:Ruby 3.2.0 Is from Another Dimension by arcturus17 on Thursday January 05, 2023 @04:41PM (#34267131)
Noted. I've read the rest of the conversation and it's very interesting.
Re:Ruby 3.2.0 Is from Another Dimension by jonnytran on Wednesday January 04, 2023 @02:28PM (#34250194)
> Ruby not having first-class functions

This is simply false. Ruby has first-class functions.

Not only that, it has blocks, which few other languages have. Ruby optimizes for the common use-case of a method that takes a single callback/piece of code, and I miss it in most other languages, including Python.

In Python, the "with" statement is super special. But in Ruby, it's just a method that takes a block. And any method can take a block, not just one per class.

https://yehudakatz.com/2010/02/07/the-building-blocks-of-rub...

Re:Ruby 3.2.0 Is from Another Dimension by empthought on Thursday January 05, 2023 @01:24PM (#34264159)
Ruby does not have first-class functions. Why do you insist on sounding so sure about something you are so wrong about?

https://blog.appsignal.com/2018/09/04/ruby-magic-closures-in...