Skip to content

Instantly share code, notes, and snippets.

View muhammadyana's full-sized avatar
🏠
Work From Anywhere

Muhammad Yana Mulyana muhammadyana

🏠
Work From Anywhere
View GitHub Profile
# handling exception
#======save page into html=======
require 'open-uri'
# web_page = open("http://indoexchanger.co.id/home")
# output = File.open("index.html", "w")
# while line = web_page.gets
# output.puts line
# end
# output.close
@muhammadyana
muhammadyana / regex.rb
Created June 8, 2017 07:30
regular expression
#thursday-08-2017
#@41studio
#Muhammad Yana Mulyana
#regex in ruby
#Operator =~ membandingkan string yg sama dan menghitung jumlah regex
# p /cat/ =~ "Dog and cat"
# p /cat/ =~ " ! # cat"
puts /aa/.class
@muhammadyana
muhammadyana / more-about-methods.rb
Last active June 8, 2017 08:50
File size, next, break and redo
def cool_dude(arg1="Miles", arg2="Coltrane", arg3="Roach")
"#{arg1}, #{arg2}, #{arg3}."
end
p cool_dude
p cool_dude("Bart")
p cool_dude("Bart", "Elwood")
p cool_dude("Bart", "Elwood", "Linus")
file = File.size("regex.rb")
p File.size("regex.rb")
@muhammadyana
muhammadyana / standar-type.rb
Last active June 8, 2017 04:19
Standar Type in Ruby
#thursday-08-2017
#@41studio
#Muhammad Yana Mulyana
#mixins in ruby
puts "Enter count of loop "
lp = gets.chomp.to_i
puts "Enter value to calculate "
val = gets.chomp.to_i
num = val
lp.times do
#thursday-08-2017
#@41studio
#Muhammad Yana Mulyana
#mixins in ruby
puts "Enter Name 1 "
name1 = gets.chomp.to_s
puts "Enter name 2 "
name2 = gets.chomp.to_s
puts "Enyer Name 3 "
#thursday-08-2017
#@41studio
#Muhammad Yana Mulyana
#inheritance in ruby
class Parent
def say_hello
puts "Hello From #{self}"
end
end
#
# require_relative 'words_from_string'
# require 'test/unit'
#
# class TestWordsFromString < Test::unit::Testcase
# def test_empty_string
# assert_equal(
# [], words_from_string("")
# )
# assert_equal(
@muhammadyana
muhammadyana / block-can-be-object.rb
Last active June 8, 2017 04:07
Block also can be an object in ruby
print "Enter parameter = "
var = gets()
class ProcExample
def pass_in_block(&action)
@stored_proc = action
end
def use_proc(parameter)
@stored_proc.call(parameter) #call the parameter
end
@muhammadyana
muhammadyana / lambda-and-proc-in-ruby.rb
Last active June 8, 2017 04:07
same as method, but it can be assign or set like a variable
print "Enter value Integer = "
int = gets.chomp.to_i
print "Enter value string = "
string = gets.chomp.to_s
def n_times(thing)
lambda {
|n|
thing * n
}
a = [1, 3, "cat"]
b = {
dog: "ben",
cat: "tom"
}
enum_a = a.to_enum #untuk
enum_b = b.to_enum
enum_a1 = a.each