A little confused: My aim is to get an array of *only* the methods I create in my class. For example, in the class below if I am successful I should see a list of three methods: [:subject, :affiliate, :address]. I know I can ask for class#methods, but that gives me a lot of inherited methods (see below). I see online that if I call class#methods(false) then I should get my desired response, however when I try this on this class, I get [], empty array. Is there a way to do this? Reason why I want this is so I can iterate my methods on initialize and set default values, to avoid @subect="" @affiliate="".... etc. Of course my real class is much bigger than three methods otherwise would not worry about it. ruby-1.9.2-p0 > class Entry ruby-1.9.2-p0 ?> attr_accessor :subject, ruby-1.9.2-p0 > :affiliate, ruby-1.9.2-p0 > :address ruby-1.9.2-p0 ?> end => nil ruby-1.9.2-p0 > e = Entry.new => #<Entry:0x2778ec0> ruby-1.9.2-p0 > e.methods(false) => [] ruby-1.9.2-p0 > e.methods => [:subject, :subject=, :affiliate, :affiliate=, :address, :address=, :taguri=, :taguri, :to_yaml_style, :to_yaml_properties, :syck_to_yaml, :to_yaml, :blank?, :present?, :presence, :duplicable?, :acts_like?, :try, :html_safe?, :`, :returning, :to_param, :to_query, :instance_values, :instance_variable_names, :copy_instance_variables_from, :to_json, :with_options, :as_json, :require_or_load, :require_dependency, :require_association, :load_dependency, :load, :require, :unloadable, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :to_sql, :equality_predicate_sql, :inequality_predicate_sql, :bind, :find_correlate_in, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :gem, :silence_warnings, :enable_warnings, :with_warnings, :silence_stderr, :silence_stream, :suppress, :class_eval, :pretty_inspect, :require_library_or_gem, :debugger, :breakpoint, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__] ruby-1.9.2-p0 > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 11, 2010, at 4:22 PM, David Kahn wrote:> A little confused: My aim is to get an array of *only* the methods I > create in my class. For example, in the class below if I am > successful I should see a list of three methods: > [:subject, :affiliate, :address].Well, I think you mean six methods as you''ll have the setters also.> > I know I can ask for class#methods, but that gives me a lot of > inherited methods (see below). I see online that if I call > class#methods(false) then I should get my desired response, however > when I try this on this class, I get [], empty array. Is there a way > to do this? Reason why I want this is so I can iterate my methods on > initialize and set default values, to avoid @subect="" > @affiliate="".... etc. Of course my real class is much bigger than > three methods otherwise would not worry about it. > > > ruby-1.9.2-p0 > class Entry > ruby-1.9.2-p0 ?> attr_accessor :subject, > ruby-1.9.2-p0 > :affiliate, > ruby-1.9.2-p0 > :address > ruby-1.9.2-p0 ?> end > => nil > ruby-1.9.2-p0 > e = Entry.new > => #<Entry:0x2778ec0> > ruby-1.9.2-p0 > e.methods(false) > => [] > ruby-1.9.2-p0 > e.methods > => > [:subject > , :subject > = > , :affiliate > , :affiliate > = > , :address > , :address > = > , :taguri > = > , :taguri > , :to_yaml_style > , :to_yaml_properties > , :syck_to_yaml > , :to_yaml > , :blank > ?, :present > ?, :presence > , :duplicable > ?, :acts_like > ?, :try > , :html_safe > ?, :` > , :returning > , :to_param > , :to_query > , :instance_values > , :instance_variable_names > , :copy_instance_variables_from > , :to_json > , :with_options > , :as_json > , :require_or_load > , :require_dependency > , :require_association > , :load_dependency > , :load > , :require > , :unloadable > , :pretty_print > , :pretty_print_cycle > , :pretty_print_instance_variables > , :pretty_print_inspect > , :to_sql > , :equality_predicate_sql > , :inequality_predicate_sql > , :bind, :find_correlate_in, :nil?, :===, :=~, :! > ~ > , :eql > ?, :hash > , :< > = > > > , :class > , :singleton_class > , :clone > , :dup > , :initialize_dup > , :initialize_clone > , :taint > , :tainted > ?, :untaint > , :untrust > , :untrusted > ?, :trust > , :freeze > , :frozen > ?, :to_s > , :inspect > , :methods > , :singleton_methods > , :protected_methods > , :private_methods > , :public_methods > , :instance_variables > , :instance_variable_get > , :instance_variable_set > , :instance_variable_defined > ?, :instance_of > ?, :kind_of > ?, :is_a > ?, :tap > , :send > , :public_send > , :respond_to > ?, :respond_to_missing > ?, :extend > , :display > , :method > , :public_method > , :define_singleton_method > , :__id__ > , :object_id > , :to_enum > , :enum_for > , :gem > , :silence_warnings > , :enable_warnings > , :with_warnings > , :silence_stderr > , :silence_stream > , :suppress > , :class_eval > , :pretty_inspect > , :require_library_or_gem > , :debugger, :breakpoint, :==, :equal?, :!, :! > =, :instance_eval, :instance_exec, :__send__] > ruby-1.9.2-p0 >In your subject line, you almost had it right, try: Entry.instance_methods(false) Here''s a quick implementation of something like you described. class Entry attr_accessor :subject, :affiliate, :address def initialize(options={}) self.class.instance_methods(false).grep(/\=/).each do |name| var = name.to_s.sub(/=$/,'''').to_sym if options.has_key?(var) puts "#{name} #{options[var]}" self.send(name, options[var]) else puts "no value given for #{var}" end end end end irb> Entry.instance_methods(false) => [:subject, :subject=, :affiliate, :affiliate=, :address, :address=] irb> e = Entry.new(:address => ''123 Main St.'') no value given for subject no value given for affiliate address= 123 Main St. => #<Entry:0x1e73f0 @address="123 Main St."> -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org +1 513-295-4739 Skype: rob.biedenharn -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Nov 11, 2010 at 4:12 PM, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org>wrote:> > On Nov 11, 2010, at 4:22 PM, David Kahn wrote: > > A little confused: My aim is to get an array of *only* the methods I create > in my class. For example, in the class below if I am successful I should see > a list of three methods: [:subject, :affiliate, :address]. > > > Well, I think you mean six methods as you''ll have the setters also. >> > I know I can ask for class#methods, but that gives me a lot of inherited > methods (see below). I see online that if I call class#methods(false) then I > should get my desired response, however when I try this on this class, I get > [], empty array. Is there a way to do this? Reason why I want this is so I > can iterate my methods on initialize and set default values, to avoid > @subect="" @affiliate="".... etc. Of course my real class is much bigger > than three methods otherwise would not worry about it. > > > ruby-1.9.2-p0 > class Entry > ruby-1.9.2-p0 ?> attr_accessor :subject, > ruby-1.9.2-p0 > :affiliate, > ruby-1.9.2-p0 > :address > ruby-1.9.2-p0 ?> end > => nil > ruby-1.9.2-p0 > e = Entry.new > => #<Entry:0x2778ec0> > ruby-1.9.2-p0 > e.methods(false) > => [] > ruby-1.9.2-p0 > e.methods > => [:subject, :subject=, :affiliate, :affiliate=, :address, :address=, > :taguri=, :taguri, :to_yaml_style, :to_yaml_properties, :syck_to_yaml, > :to_yaml, :blank?, :present?, :presence, :duplicable?, :acts_like?, :try, > :html_safe?, :`, :returning, :to_param, :to_query, :instance_values, > :instance_variable_names, :copy_instance_variables_from, :to_json, > :with_options, :as_json, :require_or_load, :require_dependency, > :require_association, :load_dependency, :load, :require, :unloadable, > :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, > :pretty_print_inspect, :to_sql, :equality_predicate_sql, > :inequality_predicate_sql, :bind, :find_correlate_in, :nil?, :===, :=~, :!~, > :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, > :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, > :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, > :protected_methods, :private_methods, :public_methods, :instance_variables, > :instance_variable_get, :instance_variable_set, :instance_variable_defined?, > :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, > :respond_to_missing?, :extend, :display, :method, :public_method, > :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :gem, > :silence_warnings, :enable_warnings, :with_warnings, :silence_stderr, > :silence_stream, :suppress, :class_eval, :pretty_inspect, > :require_library_or_gem, :debugger, :breakpoint, :==, :equal?, :!, :!=, > :instance_eval, :instance_exec, :__send__] > ruby-1.9.2-p0 > > > > In your subject line, you almost had it right, try: > Entry.instance_methods(false) > > Here''s a quick implementation of something like you described. > > class Entry > attr_accessor :subject, :affiliate, :address > def initialize(options={}) > self.class.instance_methods(false).grep(/\=/).each do |name| > var = name.to_s.sub(/=$/,'''').to_sym > if options.has_key?(var) > puts "#{name} #{options[var]}" > self.send(name, options[var]) > else > puts "no value given for #{var}" > end > end > end > end > irb> Entry.instance_methods(false) > => [:subject, :subject=, :affiliate, :affiliate=, :address, :address=] > irb> e = Entry.new(:address => ''123 Main St.'') > no value given for subject > no value given for affiliate > address= 123 Main St. > => #<Entry:0x1e73f0 @address="123 Main St."> >Ah - right, six methods. This is great, just tried, thanks!> > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > +1 513-295-4739 > Skype: rob.biedenharn > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.