0

For example,

def makeFunction name
    ... #define a function with the name supplied
end

makeFunction 'functionMade'
functionMade

I know it's possible to make a global variable $functionMade through lambda or proc, but is it possible to make it really a function without the $ prefix?

1 Answer 1

2

i think via class_eval on Kernel:

Kernel.class_eval <<-RUBY
  def abc
    puts 'abc'
  end
RUBY

but i wouldn't recommend that. what do you need it for, or are you just curious?

Sign up to request clarification or add additional context in comments.

5 Comments

Cool! I want to generate a set of functions as specified in an externally loaded JSON. :)
:D indeed! Remember eval is Evil
@albert: Have you looked at whether Struct or OpenStruct would suit your needs?
that's dangerous.. you could accidentally monkey-patch / override something that's built-in to Ruby.. Danger Will Robinson!
@AndrewGrimm: I am making an assembly code generator. There are hundreds of instructions from the datasheet. I just paste the spec such as 'ADC{S}<c><q> {<Rd>,} <Rn>, #<const>', and parse it to make functions, so that the Ruby code look like the assembly code. adc R1, R2, 100 This produce the actual assembly code, adc R1, R2, #100 I have done this in javascript, but Ruby seems to be much more close for the code generation purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.