-3
irb(main):001:0> class Fixnum
irb(main):002:1> define_method(:gimme_five) do
irb(main):003:2* 5
irb(main):004:2> end
irb(main):005:1> end
=> #<Proc:0x007ff4ed01cd10@(irb):2 (lambda)>
irb(main):006:0> class String
irb(main):007:1> define_method(:scramble) do
irb(main):008:2* new_string = self.reverse()
irb(main):009:2> new_string = new_string.upcase()
irb(main):010:2> new_string
irb(main):011:2> end
irb(main):012:1> end
=> #<Proc:0x007ff4ed02d868@(irb):7 (lambda)>
irb(main):013:0> 

I'm am not sure what I am doing wrong but my methods are not being defined???

  • Okay so my scramble method did work when I used it however in the tutorial the person got her method displayed back to her I got: # – Jeff Austin Apr 30 '15 at 02:32
  • yeah, so you should have followed the tutorial more closely and used `def` instead of `define_method`. – Sergio Tulentsev Apr 30 '15 at 06:26
  • I cannot reproduce that. When I cut&paste your code into IRb or Pry or a file, the methods get defined just fine. How exactly are you determining that your methods aren't being defined? Are you using reflection? Are you trying to call them? What happens when you use reflection? What happens when you try to call them? Do you get an error? What exactly is the error message you are getting? – Jörg W Mittag Apr 30 '15 at 15:34
  • The methods actually did end up working. I thought something was wrong because after defining my methods I got "#" in response. In the tutorial she got her method printed back out to her, and she was also using define_method. – Jeff Austin May 06 '15 at 03:56

1 Answers1

1

In ruby, it is much more common to define methods as follows (using your output as an example:

def gimme_five
  5
end

I believe that the syntax you are using to define your methods 'works', but what it is doing is creating 'lambdas' instead of regular methods. Lambdas and Procs compose Ruby's support for functional programming, and are special Ruby objects that represent blocks of code -- think of them as mini bundled up methods you can pass around to other objects.

So yeah, just define methods normally and should be fine -- the output in your irb REPL is just returning the lambda for no reason in particular -- just like how if you were to set an array in irb, you would get that array returned back:

>> a = [5]
=> [5]
Garrett Simpson
  • 1,471
  • 2
  • 17
  • 18
  • "the output in your irb REPL is just returning the lambda for no reason in particular" – IRb displays the result of evaluating the last expression entered, and the last expression is `define_method`, which returns a `Proc` object corresponding to the block that was passed to it. Similarly, `def` returns a `Symbol` corresponding to the name of the method being defined. – Jörg W Mittag Apr 30 '15 at 15:32
  • Thank you all so much for the help. And for explaining what is going on. I'm new to all this but am having a lot of fun learning this stuff. – Jeff Austin May 06 '15 at 04:00
  • When I use 'def' I don't need to use do and end? – Jeff Austin May 06 '15 at 04:02
  • Correct -- when you define a method -- which is what you are doing when you say def gimmee_five 5 end -- you do not need to say do and end. Some methods take blocks upon invocation -- A good example is the #each method on Enumerables such as Arrays and Hashes. When you are passing a multi line block, to make things more legible, you can contain the block within a do ... end statement. – Garrett Simpson May 07 '15 at 05:09
  • This is a good post that explains what I just said better: http://stackoverflow.com/questions/533008/what-is-the-difference-or-value-of-these-block-coding-styles-in-ruby – Garrett Simpson May 07 '15 at 05:17
  • Thank you, I really appreciate it. In the tutorial I was following they actually said to follow along with their way first before learning about ruby else where. I think the reason they are teaching me to use 'define_method' and having me use 'do' and 'end' is to establish fundamentals and a deeper understanding of what a block of code is and how it works. It is pretty cool to know that methods can be defined much quicker and with far less code. – Jeff Austin May 12 '15 at 07:30