Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # every thing is an object:
- 20.methods
- #=> [:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :bit_length, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :remainder, :real?, :nonzero?, :step, :quo, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :psych_to_yaml, :to_yaml, :to_yaml_properties, :find_method, :_ori_method, :ri, :system, :spawn, :howtocall, :vi, :vim, :emacs, :nano, :mate, :mvim, :ed, :methods_for, :method_lookup_path, :only_class_ancestors, :mlp, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__, :instance]
- String.class
- #=> Class
- Class.is_a? Object
- #=> true
- # Lambdas, Iterators and Ranges
- [0,1,2,3,4,5].each { |num| puts num } #=> [0, 1, 2, 3, 4, 5]
- 0
- 1
- 2
- 3
- 4
- 5
- (0..10).select(&:even?).map(&:to_s).join(' ') #=> "0 2 4 6 8 10"
- # Classes
- class Car
- attr_reader :color
- attr_reader :max_velocity
- attr_accessor :current_speed
- def initialize(color, max_velocity)
- @color = color
- @max_velocity = max_velocity
- @current_speed = 0
- end
- def drive(new_speed = max_velocity)
- raise "Can't drive that fast!" if new_speed > max_velocity
- self.current_speed = new_speed
- end
- def tune(percentage)
- @max_velocity += percentage.to_f/100 * @max_velocity
- end
- end
- # Modules
- module Breaks
- def stop
- @current_speed = 0
- end
- end
- class BetterCar < Car
- include Breaks
- end
- toyota = BetterCar.new 'blue', 50
- toyota.drive
- toyota.current_speed
- #=> 50
- toyota.stop
- toyota.current_speed
- #=> 0
- module StringExtensions
- def greet
- puts "hello " + self
- end
- end
- String.send(:include, StringExtensions) #=> String
- "Stefan".greet #=> nil
- hello Stefan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement