Code Monkey home page Code Monkey logo

piquesruby's Introduction

Piques from ruby

ruby for sysAdmins,beginning ruby ,why_guide , Learn ruby the hardWay, and more..

#Piques from: Practical Ruby for System Administrators'
# the insane hacker way. 

#inline ruby (insane)
ruby -e 'a = gets.chomp; puts a'
echo 'a = gets.chomp; puts a' > foo.rb
#interpretate file, and show some warnings
ruby -w foo.rb

#every body in ruby is an Object

puts "hello world"              #the implicit way
Kernel.puts.self "hello world"  #the explicit way, Kernel is the superclass

#the puts method is defined on IO class 
# with global objects for unix file-handles:
# $stdin, for standard input
# $stdout, for standard output
# $stderr, for standard error
# $ is for Global 

stderr.puts "Some bug issue"

### Method definition
#Hack fun, an implementation of puts

def puts(f)
	$stdout.puts("hello",f)
end
puts "folks!" 					#Override the kernel puts method, sweet ah?

# hello
# folks
#the same but without brackets
def puts word
	$stdout.puts "hello", word
end
puts "folks!" 					#Override the kernel puts method, sweet ah?

# Class definition
class foo
end

#Method declaration
def foo
end

##Define a class  whith a method 
class Why
	def speak
		"chuncky bacon"
	end
end

#use the class for an explicit large way

f_says = Why.new
puts f_says.speak 

#chunky bacon

#implicit short way :
puts Why.new.speak

## Class Definition with data attributes
#initialize method is called with new
#and passes all arguments to initializate,
#@someAttribute is an instance variable 
#@ is for object variable from some class

class Dog
  def initialize meters years
    @size = meters
	@age = years
  end
  
  def grow meters
    @size += meters
  end
  
  def speak
  	if @size < 0.2 then
		"yap"
	elsif @size < 1 then
		"woof woof"
  	else
	    "ROOOOOAAARRRRR!"
	end
  end #end def
  
  def show_age
  	return @age
  end
end

#create a dog and initialize their size

boby = Dog.new 0.3        # instance a new dog , and set size = 0.3
boby.initializate 0.3, 15 # observe the dinamic type
boby.show_age             # 15
boby.speak 			      # "woof woof"
boby.grow 34       
boby.speak 			      # "ROOOOOAAARRRRR!"

## More basics for OOP in ruby
# return and setting variables

class Building
  #some methods here

  def height
  	return @heigth
  end
  
#setting heigth with some external value
  def height meters
  	@heigth = meters
  end
end

e = Building.new
e.heigth = 440		#setting a value
e.heigth 			#override the method and return the value

# But! Its too noisy have to write this all time!!
# for more fun writting code Matz implement Accessor Macros
# What the hack is an Accessor Macro?
# a set of class and methods to wrapp and automatize tasks
# attr_reader, create a getter Method
# attr_writer, create a setter Method 
# attr_accessor, create both methods

class Foobar
 attr_accessor :foo
 attr_accessor :bar
end

f = Foobar.new
f.foo = "The answer of all questions"
f.bar = 42
puts f.foo + f.bar.to_s 

#the answer of all questions 42
#:foo, :bar are symbols for some unique object attribute

### Blocks page 33 ,"Practical Ruby for System Administrators"

piquesruby's People

Contributors

sebastianlujan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.