I'm very new to Ruby so please go easy on me. I have this small function that doesn't want to perform an intersection command. If I go into irb and enter the arrays then set the intersection command like: third_array = array1 & array2, third_array returns the common element. But when I run this snippet through irb, it just returns [ ]. Any suggestions?
class String
define_method(:antigrams) do |word2|
array1 = []
array2 = []
array1.push(self.split(""))
array2.push(word2.split(""))
third_array = array1 & array2
third_array
end
end
define_methodhere whendefwould do the job.define_methodis reserved for cases where you don't know the name of the method in advance, it's computed, or the block inside is generated dynamically somehow.define_methodis introduced beforedef(i.e.,def antigrams(word2))? I ask because, as @tadman mentions, usingdefine_methodhere is a bit odd, but I could see where an instructor might believe it's useful to coverdefine_methodbeforedef.define_methodis intended for meta-programming, that is code that writes code, so dropping that people on week one is really throwing them into the deep end. Every introduction I've ever seen or written always starts withdef.