# Why is Ruby dynamic?

**URL:** <https://rubytalk.org/t/why-is-ruby-dynamic/48894>\
**Category:** ruby-talk\
**Created:** [6 September 2008 01:16 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894 "2008-09-06T01:16:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [6 September 2008 01:16 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/1 "2008-09-06T01:16:54Z")

</div>

People say Ruby is a dynamic language. I don't understand what the  
"dynamic" mean here. Does this mean Ruby can dynamic change itself at  
run-time?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Adam2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/james_adam2/32/1989_2.png) [@James\_Adam2](https://rubytalk.org/u/James_Adam2)\
**Post date:** [6 September 2008 16:11 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/2 "2008-09-06T16:11:09Z")

</div>

Essentially, yes. You might consider it sort-of-opposite to a 'compiled' language, though that's less strictly true. Ruby objects are completely malleable as your program runs.

> **···**
>
> On 6 Sep 2008, at 02:16, Zhao Yi wrote:
> 
> > People say Ruby is a dynamic language. I don't understand what the  
> > "dynamic" mean here. Does this mean Ruby can dynamic change itself at  
> > run-time?

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [9 September 2008 01:19 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/3 "2008-09-09T01:19:18Z")

</div>

Zhao Yi wrote:

> People say Ruby is a dynamic language. I don't understand what the  
> "dynamic" mean here. Does this mean Ruby can dynamic change itself at  
> run-time?

It means Ruby can change its objects at runtime. For example:

&nbsp;&nbsp;&nbsp;class Narcissus  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def call\_once  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Narcissus  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def call\_once  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "nope!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

The first time you call Narcissus.new.call\_once, it will not raise. The second time, it will raise "nope!". Ruby allows class definitions to change on the fly.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;Phlip

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 September 2008 17:26 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/4 "2008-09-06T17:26:11Z")

</div>

There are several aspects to Ruby's dynamism: methods and classes can  
change at runtime, variables are dynamically typed and generally as  
implementing is often faster than in other languages Ruby is very  
dynamic (some call it "agile").

Kind regards

robert

> **···**
>
> 2008/9/6 James Adam \<james@lazyatom.com\>:
> 
> > On 6 Sep 2008, at 02:16, Zhao Yi wrote:
> > 
> > > People say Ruby is a dynamic language. I don't understand what the  
> > > "dynamic" mean here. Does this mean Ruby can dynamic change itself at  
> > > run-time?
> > 
> > Essentially, yes. You might consider it sort-of-opposite to a 'compiled'  
> > language, though that's less strictly true. Ruby objects are completely  
> > malleable as your program runs.
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![David\_Masover](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Masover](https://rubytalk.org/u/David_Masover)\
**Post date:** [9 September 2008 03:10 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/5 "2008-09-09T03:10:38Z")

</div>

A nitpick, but... string evals considered harmful!

> &nbsp;&nbsp;&nbsp;class Narcissus  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def call\_once  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval '  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Narcissus  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def call\_once  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "nope!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end

I'd write this as:

class Narcissus  
&nbsp;&nbsp;def call\_once  
&nbsp;&nbsp;&nbsp;&nbsp;def call\_once  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise 'nope!'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

But perhaps the more impressive demos are modifying system classes --  
re-opening String, for instance -- or eigenclass hacks, or dynamically  
generating functions. I always loved define\_method. When you know exactly  
what kind of things are acceptable:

class PunchCard  
&nbsp;&nbsp;%w(fold bend mutilate).each do |action|  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method action do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Don't #{action} me!"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

And when you don't necessarily know, there's method\_missing:

class TwoYearOld  
&nbsp;&nbsp;def method\_missing method, \*args  
&nbsp;&nbsp;&nbsp;&nbsp;puts "That's MY #{method}!"  
&nbsp;&nbsp;end  
end

And Ruby makes it very difficult to build a cheat-proof robot game, as you can  
always start rewriting the other robot:

class MyRobot  
&nbsp;&nbsp;def move  
&nbsp;&nbsp;&nbsp;&nbsp;class \<\< other\_robot  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def move  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise 'Explode!'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Of course, it's comforting to know that string evals are there -- having a  
working "eval" is a sign of a truly dynamic language. (Probably makes irb  
easier to write, too!) But there's very few legitimate uses of it anymore, I  
think.

> **···**
>
> On Monday 08 September 2008 20:19:18 Phlip wrote:

---

<div class="post-metadata">

**Author:** ![Zhao\_Yi](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@Zhao\_Yi](https://rubytalk.org/u/Zhao_Yi)\
**Post date:** [7 September 2008 02:04 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/6 "2008-09-07T02:04:41Z")

</div>

Robert Klemme wrote:

> **···**
>
> > 2008/9/6 James Adam \<james@lazyatom.com\>:
> > 
> > > On 6 Sep 2008, at 02:16, Zhao Yi wrote:
> > > 
> > > > People say Ruby is a dynamic language. I don't understand what the  
> > > > "dynamic"e$B!!e(Bmean here. Does this mean Ruby can dynamic change itself at  
> > > > run-time?
> > > 
> > > Essentially, yes. You might consider it sort-of-opposite to a 'compiled'  
> > > language, though that's less strictly true. Ruby objects are completely  
> > > malleable as your program runs.
> > 
> > There are several aspects to Ruby's dynamism: methods and classes can  
> > change at runtime, variables are dynamically typed and generally as  
> > implementing is often faster than in other languages Ruby is very  
> > dynamic (some call it "agile").
> > 
> > Kind regards
> > 
> > robert
> 
> It is also same for other scripts like perl, python, so they are all  
> dynamic language?  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [9 September 2008 10:58 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/7 "2008-09-09T10:58:40Z")

</div>

David Masover wrote:

> A nitpick, but... string evals considered harmful!

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval '

> I always loved define\_method.

I recently tried define\_method and it didn't work, so I fell back to eval. (Note to the newbs: Try things the most progressive way possible, such as a fixed version of Dave's version of my example, before trying a hack...)

Here's the lines, from inspect\_sql:

&nbsp;&nbsp;&nbsp;def self.\_decorate\_explainers(explains) #:nodoc:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def explains.find\_statements(regexp)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return AssertEfficientSql.\_decorate\_explainers(select{|explanation|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explanation[:statement] =~ regexp  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(\_multiples + \_singulars).each do |rollup, explainer|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "def explains.#{rollup}; map{|q|q[:#{rollup}]}.flatten.uniq; end"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return explains  
&nbsp;&nbsp;&nbsp;end

For the eval line, I first tried explains.define\_method, and it didn't work. Instead of researching why, or finding a kindler gentler system, I whipped out eval and put a bullet thru the problem.

It's the shortest possible eval, anyway!

Also I think you can't say 'def foo; def bar; end; end' - the def is in method-space, not class-space.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;Phlip

---

<div class="post-metadata">

**Author:** ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)\
**Post date:** [7 September 2008 05:04 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/8 "2008-09-07T05:04:15Z")

</div>

Yup.

> **···**
>
> 2008/9/6 Zhao Yi \<youhaodeyi@gmail.com\>:
> 
> > It is also same for other scripts like perl, python, so they are all  
> > dynamic language?
> 
> --  
> Avdi
> 
> Home: [http://avdi.org](http://avdi.org)  
> Developer Blog: [http://avdi.org/devblog/](http://avdi.org/devblog/)  
> Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
> Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [9 September 2008 11:53 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/9 "2008-09-09T11:53:30Z")

</div>

Hi --

> David Masover wrote:
> 
> > A nitpick, but... string evals considered harmful!
> 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval '
> 
> > I always loved define\_method.
> 
> I recently tried define\_method and it didn't work, so I fell back to eval. (Note to the newbs: Try things the most progressive way possible, such as a fixed version of Dave's version of my example, before trying a hack...)
> 
> Here's the lines, from inspect\_sql:
> 
> def self.\_decorate\_explainers(explains) #:nodoc:  
> &nbsp;&nbsp;&nbsp;def explains.find\_statements(regexp)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return AssertEfficientSql.\_decorate\_explainers(select{|explanation|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explanation[:statement] =~ regexp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;(\_multiples + \_singulars).each do |rollup, explainer|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval "def explains.#{rollup}; map{|q|q[:#{rollup}]}.flatten.uniq; end"  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;return explains  
> end
> 
> For the eval line, I first tried explains.define\_method, and it didn't work. Instead of researching why, or finding a kindler gentler system, I whipped out eval and put a bullet thru the problem.

define\_method is a private instance method of Module, so a typical use  
follow something like this path:

&nbsp;&nbsp;&nbsp;c.class\_eval { define\_method(m) {|x| puts x } }

> It's the shortest possible eval, anyway!
> 
> Also I think you can't say 'def foo; def bar; end; end' - the def is in method-space, not class-space.

You can nest def's, but the nested one (once the outer one is  
executed) becomes an outer one:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hi from y"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;c = C.new  
&nbsp;&nbsp;&nbsp;c.x  
&nbsp;&nbsp;&nbsp;c.y # Hi from y

&nbsp;&nbsp;&nbsp;d = C.new  
&nbsp;&nbsp;&nbsp;d.y # Hi from y

David

> **···**
>
> On Tue, 9 Sep 2008, Phlip wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL  
> &nbsp;&nbsp;&nbsp;Advancing with Rails January 19-22 Fort Lauderdale, FL \*  
> &nbsp;&nbsp;&nbsp;\* Co-taught with Patrick Ewing!  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![David\_Masover](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Masover](https://rubytalk.org/u/David_Masover)\
**Post date:** [9 September 2008 15:33 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/10 "2008-09-09T15:33:57Z")

</div>

> David Masover wrote:
> 
> \> A nitpick, but... string evals considered harmful!

This is worth bearing. I don't have a problem with eval, only with string  
eval. Given that:

> For the eval line, I first tried explains.define\_method, and it didn't work.

According to Philip, define\_method is a private method, so you can't do that  
from outside that object.

Assuming "explains" is a class or a module, you could always go inside the  
object:

explains.module\_eval do  
&nbsp;&nbsp;define\_method :foo ...  
&nbsp;&nbsp;# More methods?  
&nbsp;&nbsp;define\_method :bar ...  
end

See, I love those, because they can actually take blocks, not just strings.  
Since I'd mostly just be passing a variable in through string interpolation,  
anyway, it's not only faster, it's safer -- SQL injection is enough of a  
problem, we don't need Ruby injection, too!

If it's only a one-off, and you find that eval to be too verbose, there's  
always the send hack:

explains.send :define\_method, ...

This works mostly because send doesn't check for whether a given method is  
actually reachable at that point -- I think you need public\_send for that.  
Obviously, send is useful for more than just that...

The main legitimate solution I've found is cases like ActiveSupport's  
String#constantize -- this is a case where it would take quite a lot more  
Ruby to accomplish the same thing as a simple eval. And even here, it's  
surrounded by sanity checks, and it's isolated, so that you never have to  
know it's there (I didn't, till I looked at the source of constantize out of  
curiosity).

And there's always the performance -- I consider evals to be acceptable when  
run once, during program start (or at some arbitrary point after that). It's  
still code smell, but constantize is an example where the alternative might  
be worse. But I'd never call constantize in a Rails view.

> **···**
>
> On Tuesday 09 September 2008 05:58:40 Phlip wrote:

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [7 September 2008 05:39 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/11 "2008-09-07T05:39:37Z")

</div>

Hi,

> \> It is also same for other scripts like perl, python, so they are all  
> \> dynamic language?
> 
> Yup.

Try to do something like this in Python:

&nbsp;&nbsp;class Time  
&nbsp;&nbsp;&nbsp;&nbsp;def german  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strftime "%d.%m.%Y, %X"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;class Numeric  
&nbsp;&nbsp;&nbsp;&nbsp;def MB  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self/1024.0/1024.0  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

It's dynamic because it produces dynamic programmers.

Bertram

> **···**
>
> Am Sonntag, 07. Sep 2008, 14:04:15 +0900 schrieb Avdi Grimm:
> 
> > 2008/9/6 Zhao Yi \<youhaodeyi@gmail.com\>:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin1](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Chad\_Perrin1](https://rubytalk.org/u/Chad_Perrin1)\
**Post date:** [9 September 2008 00:42 UTC](https://rubytalk.org/t/why-is-ruby-dynamic/48894/12 "2008-09-09T00:42:29Z")

</div>

> Hi,
> 
> \> \> It is also same for other scripts like perl, python, so they are all  
> \> \> dynamic language?  
> \>  
> \> Yup.
> 
> Try to do something like this in Python:
> 
> &nbsp;&nbsp;class Time  
> &nbsp;&nbsp;&nbsp;&nbsp;def german  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strftime "%d.%m.%Y, %X"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;class Numeric  
> &nbsp;&nbsp;&nbsp;&nbsp;def MB  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self/1024.0/1024.0  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end

Python is "dynamic" compared to, say, C or Java. People tend to want to  
classify languages in a black/white manner, putting some languages on one  
side of a dividing line between "dynamic" and "static", and other  
languages on the other side. The way most people view these things,  
Perl, Python, and Ruby are all "dynamic".

That doesn't mean they're all \*equally\* dynamic, of course -- as you  
point out. The fact they aren't all equally dynamic doesn't mean Python  
isn't "dynamic" as well.

> It's dynamic because it produces dynamic programmers.

That's certainly one way to look at it -- and it does indeed seem to at  
least facilitate, if not produce, "dynamic programmers".

> **···**
>
> On Sun, Sep 07, 2008 at 02:39:37PM +0900, Bertram Scharpf wrote:
> 
> > Am Sonntag, 07. Sep 2008, 14:04:15 +0900 schrieb Avdi Grimm:  
> > \> 2008/9/6 Zhao Yi \<youhaodeyi@gmail.com\>:
> 
> --  
> Chad Perrin [content licensed PDL: [http://pdl.apotheon.org](http://pdl.apotheon.org) ]  
> Olin Shivers: "I object to doing things that computers can do."
