Skip to content

Instantly share code, notes, and snippets.

View boddhisattva's full-sized avatar
🌷
Vasudhaiva Kutumbakam 🙂

Mohnish G J boddhisattva

🌷
Vasudhaiva Kutumbakam 🙂
View GitHub Profile
@boddhisattva
boddhisattva / conditionals.rb
Created February 19, 2014 05:43
Conditionals in Ruby
# Conditionals
a = 5
b = 3
# if else in operation..
if a < b
puts("true")
else
@boddhisattva
boddhisattva / iterators.rb
Last active August 29, 2015 13:56
Iterators in Ruby
# working with iterators
3.times { puts "thank you!" }
puts("\n")
data = [1,2,3,4,5]
data.each {|x| puts x} # note how x although its not being declared or defined.. works for each element of the array data..
@boddhisattva
boddhisattva / loops.rb
Created February 19, 2014 05:55
Loops in Ruby
# loops in Ruby
# While
x = 10
while x >= 0 do
puts x
x = x - 1
end
@boddhisattva
boddhisattva / enumerables.rb
Created February 19, 2014 06:09
Enumerable Objects in Ruby
#Playing with Enumerable Objects
squares = [1,2,3].collect { |x| x*x }
print("Squares:")
puts(squares)
puts("\n")
#--------------------------------------------------------------------------------------------------------------------------------
print("Odd numbers upto range:\n")
@boddhisattva
boddhisattva / datagram_server.rb
Last active August 29, 2015 14:01
Chat Application
require 'socket'
#this is like the chat room..
port = ARGV[0]
ds = UDPSocket.new
ds.bind(nil,port)
$b = IO.new(2,"w+")
@boddhisattva
boddhisattva / datagram_client.rb
Created May 11, 2014 11:35
Chat application - client script
require 'socket'
host, port = ARGV
ds = UDPSocket.new
ds.connect(host,port)
request_message = ''
$a = IO.new(2,"w+")
@boddhisattva
boddhisattva / rvm cmds
Created May 25, 2014 16:20
RVM commands output
rvm list
rvm rubies
ruby-1.9.3-p327 [ i686 ]
ruby-1.9.3-p374 [ i686 ]
ruby-1.9.3-p392 [ i686 ]
=> ruby-2.0.0-p451 [ i686 ]
ruby-2.1.1 [ i686 ]
* ruby-2.1.2 [ i686 ]
@boddhisattva
boddhisattva / sample.rb
Created May 26, 2014 01:48
Active Record Connection Exeute to return an array of hashes
2.0.0-p451 :036 > ActiveRecord::Base.connection.execute('SELECT * FROM microposts').each(as: :hash)
(0.3ms) SELECT * FROM microposts
=> [{"id"=>1, "content"=>"a", "user_id"=>1, "created_at"=>2014-05-24 09:50:57 UTC, "updated_at"=>2014-05-24 09:50:57 UTC},
{"id"=>2, "content"=>"", "user_id"=>1, "created_at"=>2014-05-24 09:51:02 UTC, "updated_at"=>2014-05-24 09:51:02 UTC},
{"id"=>3, "content"=>"", "user_id"=>1, "created_at"=>2014-05-24 09:51:06 UTC, "updated_at"=>2014-05-24 09:51:06 UTC},
{"id"=>4, "content"=>"", "user_id"=>2, "created_at"=>2014-05-24 09:51:09 UTC, "updated_at"=>2014-05-24 09:51:09 UTC},
{"id"=>5, "content"=>"", "user_id"=>3, "created_at"=>2014-05-24 09:51:13 UTC, "updated_at"=>2014-05-24 09:51:13 UTC},
{"id"=>6, "content"=>"", "user_id"=>4, "created_at"=>2014-05-24 09:51:17 UTC, "updated_at"=>2014-05-24 09:51:17 UTC},
@boddhisattva
boddhisattva / Delete Reminder
Last active August 29, 2015 14:05
Reminder API
Delete Reminder
DELETE /reminders/{id}
Deletes a reminder.
Status Codes
Success: 204
Example
@boddhisattva
boddhisattva / singly_linked_list.rb
Last active August 29, 2015 14:10
Original Singly Linked List
class Node
attr_accessor :data, :next_node
end
class LinkList
attr_accessor :head
def initialize(val)
@head = get_node(val)
end