Friday, December 28, 2012

array methods



To get methods used for arrays in ruby, do puts myarray.methods
inspect
to_s
to_a
to_ary
frozen?
==
eql?
hash
[]
[]=
at
fetch
first
last
concat
<<
push
pop
shift
unshift
insert
each
each_index
reverse_each
length
size
empty?
find_index
index
rindex
join
reverse
reverse!
rotate
rotate!
sort
sort!
sort_by!
collect
collect!
map
map!
select
select!
keep_if
values_at
delete
delete_at
delete_if
reject
reject!
zip
transpose
replace
clear
fill
include?
<=>
slice
slice!
assoc
rassoc
+
*
-
&
|
uniq
uniq!
compact
compact!
flatten
flatten!
count
shuffle!
shuffle
sample
cycle
permutation
combination
repeated_permutation
repeated_combination
product
take
take_while
drop
drop_while
pack
entries
sort_by
grep
find
detect
find_all
flat_map
collect_concat
inject
reduce
partition
group_by
all?
any?
one?
none?
min
max
minmax
min_by
max_by
minmax_by
member?
each_with_index
each_entry
each_slice
each_cons
each_with_object
chunk
slice_before
nil?
===
=~
!~
class
singleton_class
clone
dup
initialize_dup
initialize_clone
taint
tainted?
untaint
untrust
untrusted?
trust
freeze
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__

print files in directory



Running this script will give you the contents of a given directory (in linux)

#---
# Excerpted from "Everyday Scripting in Ruby"
# Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
#---

puts Dir.glob('**/*').each { | file | file.downcase }.sort

Friday, December 21, 2012

random generate this or that



#!/usr/bin/ruby -w

puts thisOrThat = rand(2) == 0? 4 : 5

puts ors = 4+rand(2)

puts ors2 = %w(down up).sample

puts rand(4..5)

Monday, December 17, 2012

random password generator


#!/usr/bin/ruby -w
# random password generator

require 'securerandom'

puts "enter max number of char: "
chars = gets.chomp.to_i
newnum = rand(36 ** chars).to_s(36) #sub chars with max req length of password
puts "your new password is #{newnum}"
othernum = SecureRandom.urlsafe_base64(chars)
puts "...or if you want " + othernum

Saturday, December 15, 2012

while !condition sleep



while (!condition) 
  sleep(10)
end

# also try until, unless, if-else

Thursday, December 13, 2012

strings - format character



#!/usr/bin/ruby -w

name2 = "joe"
nam3 = 'mary'
puts "hello #{name2}, where is #{nam3}"
puts "hi %s. this is %s" % [name2, nam3]