Skip to content

Instantly share code, notes, and snippets.

View Quemia's full-sized avatar

Quêmia Quemia

View GitHub Profile

Case

used when there is a need to evaluate a large number of conditions.

Ex:

    def identify_class(obj)
      case obj
      when Hacker,Submission,TestCase,Contest

puts "It's a #{obj.class}!"

Until

Executes the code while conditional is false.

Until structure:

until conditional [do]
  code

end

Infinite Loop

Creates an infinite loop.

Ex:

 irb(main):007:1* loop do

irb(main):008:1* puts Time.now

Unless

Executes the code if conditional is false.

simple example:

    a = 1
    unless 4 > 1

puts "Welcome"

&

Returns a new actress containing elements common to both matrices; The order is preserved in the original matrix.

Ex:

    [1,1,2,2,3,5,] & [1,2,2,5,6]

=> [1,2,5]

Each

Executes each element of the logic passed in the block.

Ex:

def scoring(array)
  array.each do |user| 
    user.update_score

end

Abs()

The abs() function in ruby returns the absolute value of the integer. ex:

  num1 = -21   =>  puts (num1).abs  => 21
  num2 = 23    =>  puts (num2).abs  => 23  

<=>()

A simple even and odd function

puts "type a number: "
number = gets.chomp.to_i #getting entire entries
if ((number%2)==0)
  puts "#{number} is even"
else
  puts "#{numero} is odd"   
end
String = "The Ruby Language"
string.split("").shuffle.join

Split

splits a string into pieces, returning an array of smaller strings.

Shuffle

%Q

Used as a string, it works as double quotes.

%Q[hello] 
>> "Hello"

%q