Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Eval
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Ruby === The [[Ruby (programming language)|Ruby programming language]] interpreter offers an <code>eval</code> function similar to Python or Perl, and also allows a [[Scope (programming)|scope]], or [[Name binding|binding]], to be specified. Aside from specifying a function's binding, <code>eval</code> may also be used to evaluate an expression within a specific class definition binding or object instance binding, allowing classes to be extended with new methods specified in strings. <syntaxhighlight lang="ruby"> a = 1 eval('a + 1') # (evaluates to 2) # evaluating within a context def get_binding(a) binding end eval('a+1',get_binding(3)) # (evaluates to 4, because 'a' in the context of get_binding is 3) </syntaxhighlight> <syntaxhighlight lang="ruby"> class Test; end Test.class_eval("def hello; return 'hello';end") # add a method 'hello' to this class Test.new.hello # evaluates to "hello" </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)