i playing around w/ruby , attempting create function reason not working way thought would. not sure why i'm having problem here code :
class script print "enter number: " number = gets def random (temp) puts "#{temp}" puts "inside function" end random (number) end
error :
script.rb:13:in `<class:script>': undefined method `random' script:class (nomethoderror) script.rb:1:in `<main>'
the problem is, define random
method instance method, try call on class level. have 2 options fix this:
make class method (note self
):
class script def self.random(temp) puts "#{temp}" puts "inside function" end print "enter number: " number = gets random(number) end
or make method create instance first (note new
):
class script def random(temp) puts "#{temp}" puts "inside function" end print "enter number: " number = gets new.random(number) end
Comments
Post a Comment