Ruby: Difference between revisions
Jump to navigation
Jump to search
(→Object) |
(→Object) |
||
Line 40: | Line 40: | ||
Bye Nicolas, come back soon. | Bye Nicolas, come back soon. | ||
=> nil</pre> | => nil</pre> | ||
====Methodes of a particular Object==== | |||
* Current Object and parent | |||
<pre>irb>Greeter.instance_methods | |||
=> [:say_hi, :say_bye, :m, :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, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]</pre> | |||
* Current Object only | |||
<pre>irb>Greeter.instance_methods(false) | |||
=> [:say_hi, :say_bye]</pre> |
Revision as of 23:35, 12 November 2014
First Step
Call Ruby command line
irb
Methode
- Define a methode "m"
def m(name = "World") puts "Hello Mr #{name.capitalize}!" end
- Execute
m("nicolas")
Hello Mr Nicolas! => nil
Class
class Greeter def initialize(name = "World") @name = name end def say_hi puts "Hi #{@name}!" end def say_bye puts "Bye #{@name}, come back soon." end end
- class define a new class
- @name is a variable d'instance
Object
irb>o = Greeter.new("Nicolas") => #<Greeter:0x00000002579a48 @name="Nicolas">
irb>o.say_hi Hi Nicolas! => nil
irb> o.say_bye Bye Nicolas, come back soon. => nil
Methodes of a particular Object
- Current Object and parent
irb>Greeter.instance_methods => [:say_hi, :say_bye, :m, :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, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
- Current Object only
irb>Greeter.instance_methods(false) => [:say_hi, :say_bye]