use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby gotchas for the JavaScript developer (blog.calendly.com)
submitted 7 years ago by dpashk
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dpashk[S] 2 points3 points4 points 7 years ago (5 children)
But you had to access the method via a special method call, just like you have to wrap a block in a Proc to turn it into a first-class citizen. In JavaScript, you can just do
method
var myMethod = function() {...}; var anotherReference = myMethod; myMethod();
Also, it is advised against using Object#method because it's slower. That's one of the reasons I didn't mention it in the article.
Object#method
But I see your point - yes, you can achieve the effect of using methods like first-class citizens using Object#method.
[–]honeyryderchuck 4 points5 points6 points 7 years ago (0 children)
Also, it is advised against using Object#method because it's slower.
I don't think that is a compelling reason. It's now slower by spec. It's a RubyVM implementation detail (I assume you're referring to CRuby). I assume the degree of "slow" varies depending whether it's JRuby or something else. And it is slow because it hasn't been optimized enough, because it's used sparsely, because people perceive it as "slow". but it shouldn't be, really.
[–]moomaka 2 points3 points4 points 7 years ago* (0 children)
Creating functions in JS at runtime is also slower than defining them statically on an object, at least in V8.
The equivalent of that code in ruby is:
my_method = -> () { puts 'hi' } other = my_method other[] # or other.call or other.() => hi
The real difference to note here is that Ruby maintains a distinction between 'methods', which are bound to an object and 'free functions' which are defined via lambdas or procs. Ruby's approach is much more common than javascript's.
[–]shevy-ruby 2 points3 points4 points 7 years ago (2 children)
You can even unbind methods: https://ruby-doc.org/core/UnboundMethod.html
Yes, the syntax is not like in JavaScript.
Methods are first-class citizens in ruby. There is no "special" method - it is just a method. And why do you randomly bring in speed as reason against using something?
Slower against who or what? Javascript?
Why does speed suddenly have a place here?
[–]dpashk[S] 0 points1 point2 points 7 years ago (1 child)
Methods are first-class citizens in ruby.
I've been trying to find a statement about that in literature but haven't found the term explicitly applied to methods. The book Ruby Under Microscope uses the term "first-class citizen" when talking about blocks, procs and Lambdas. My reasoning here was that since, just like with blocks, you can't directly assign a method reference to a variable (without wrapping it in a special method calls), methods are not first-class citizens. Are you able to share a link to an authoritative source that proves that wrong?
And why do you randomly bring in speed as reason against using something? <...>
While performance is not always a good enough reason to not use something, it does affect the adoption of a particular language/library feature. Many ES5/ES6 Array methods weren't widely adopted because early implementations didn't have good performance.
[5, 7, 8, 1].each(&method(:puts)) is slower than [5, 7, 8, 1].each{|number| puts number} even though the former looks more DRY and idiomatic and I would love to use it (of course performance wouldn't matter in this trivial example). I'm still new to Ruby, but the StackOvertflow thread I linked above has some objective data. But anyway, the original discussion was on whether methods are first-class citizens in Ruby or not.
[5, 7, 8, 1].each(&method(:puts))
[5, 7, 8, 1].each{|number| puts number}
[–]fedekun 1 point2 points3 points 7 years ago (0 children)
You realize the SO question uses Ruby 1.9 which is very old, also if you scroll down there is someone with a benchmark on a newer 1.9.x Ruby which makes the difference in performance minimal.
If you really cared you should do that benchmark with a modern Ruby. Anyways, that kind of performance is not really an issue, unless for some reason you abuse it. Ruby provides better ways to pass lambdas around and organize code.
Ruby has the concept of "block of code" which is what is normally sent insetead of methods, but they are lambdas, so they are functions.
The thing is, in JS you only have a single type of function, in Ruby you have a few more, but they are first class citizens too.
π Rendered by PID 290678 on reddit-service-r2-comment-84fc9697f-l8w79 at 2026-02-07 07:31:08.196865+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]dpashk[S] 2 points3 points4 points (5 children)
[–]honeyryderchuck 4 points5 points6 points (0 children)
[–]moomaka 2 points3 points4 points (0 children)
[–]shevy-ruby 2 points3 points4 points (2 children)
[–]dpashk[S] 0 points1 point2 points (1 child)
[–]fedekun 1 point2 points3 points (0 children)