Ruby: Difference between revisions
Jump to navigation
Jump to search
(→Object) |
|||
Line 12: | Line 12: | ||
* Execute | * Execute | ||
m("nicolas") | <pre>m("nicolas")</pre> | ||
<pre>Hello Mr Nicolas! | <pre>Hello Mr Nicolas! | ||
=> nil</pre> | => nil</pre> | ||
=== Class === | === Class === | ||
<pre> | <pre> |
Revision as of 01:58, 13 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]
Validate Object Methode
irb>o.respond_to?("name") => false
o.respond_to?("say_hi") => true
o.respond_to?("to_s") => true