Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

Coming from C (no ++) and Java the first thing I noticed after a month with Ruby (in 2005?) was that I didn't write any for loop. I mean, those useless for (i = 0; i < length (array); i++). They become array.each. Then I loved the blocks and later on the postfix if and unless. It's not perfect, it's got its quirks and metaprogramming sometimes is easier in Python, sometimes not, but overall it's the easiest language I used so far. I never tried Crystal because of the compilation step. I really don't need the extra speed for my sw and no customer asked me Crystal so far. I guess it's going to be a similarly pleasant experience.


> metaprogramming sometimes is easier in Python, sometimes not

Now, I've been exposed to more ruby, and more metaprogramming in ruby, than in python - but this stil surprises/intrigues me.

What kind of metaprogramming do you find easier in python?


If I'm not wrong I can add a methods like this

  def m2():
   print("nuovo")

  object.new_method = m2
instead of redefining the class. It's similar to JavaScript. However I don't have a computer at hand now. I can't check the details and what could be done on Ruby.


This may be what you're looking form, to add a method to a single instance, although the syntax is clearly not as smooth: https://stackoverflow.com/questions/1887845/add-method-to-an...

OTOH, if your problem is slightly different, I find the syntax to add methods to any class very practical and powerful, with of course the caveat that you can produce a maintainability nightmare if overused:

  irb(main):001:0> a = 1
  => 1
  irb(main):002:0> class Integer
  irb(main):003:1>   def hello
  irb(main):004:2>     puts "world"
  irb(main):005:2>   end
  irb(main):006:1> end
  => :hello
  irb(main):007:0> a.hello
  world
  => nil


That’s a pretty shallow view of easier metaprogramming. Ruby allows for many different ways to metaprogram. You can open up any class at any time and create new methods on the class, you can even add a new method directly on an individual instance of a class without changing any other instantiated objects of the same class. I’m just scratching the surface of what’s available wrt metaprogramming in ruby. The ease of metaprogramming in ruby is specifically why rails was easy to write.


True, unfortunately. You can't pass functions around as first class objects, you'll have to use lambdas or procs for that (although even then composition doesn't work like that in Ruby).

(No judgement from me on whether that's better/worse/about equal, just confirming.)


I suppose in ruby you'd re-open Object and add new_method there... (in part by overriding method_missing, maybe).

I wouldn't recommend actually doing that, however.

If actually doing this (adding new method to a class) I think it's quite easy to re-open a class in ruby (see sibling comment(s)).

I can see adding methods actual instances (only) might be more work in ruby though.

What would be the typical use case for a special instance in a given scope, as opposed to a special class in a given scope?

Ed: i see qquark's so link address the technical part: define_singleton_method (so no need to open Object to add that...)




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: