Ruby’s yielding methods
To demonstrate a yielding method, I created A user-management class containing registered_users method. The method will return an array of the registered user’s hash if no block is given. Otherwise […]
To demonstrate a yielding method, I created A user-management class containing registered_users method. The method will return an array of the registered user’s hash if no block is given. Otherwise […]
When should I write modules in a Ruby project? The short answer is whenever you have have an implementation that is “something-able”. Abstract classes in C++, Java or Protocols in […]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class MountedPartitions attr_reader :list def initialize @list = [] lines = %x[mount].split("\n") lines.each { |line| line_parts = line.split(" ") @list << {:partition_path => line_parts[0], :mount_point => line_parts[2], :type => line_parts[4]} } rescue raise RuntimeError, "Probably not running a NIX system" end end |
For example…
1 2 3 4 5 6 7 8 9 |
mp = MountedPartitions.new mp.list.each { |p| puts p[:partition_path] puts p[:mount_point] puts p[:type] } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#!/usr/bin/env ruby require 'find' module Find def match(what, *paths) what.downcase! find(*paths) { |path| begin puts path if File.file?(path) && File.readable?(path) && File.basename(path).downcase.include?(what) rescue Exception => e puts e.message end } end module_function :match end if ARGV.empty? puts "Where Is the Damn File" puts "usage: widf a_file_to_find" exit end Find.match(ARGV.join(" "), "/") |
The example object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
require 'yaml' class Person attr_accessor :first_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def save_to_file(file_path) File.open(file_path, "w") { |f| f.write(self.to_yaml) } end def self.load_from_file(file_path) p = YAML::load_file(file_path) end end |
Now lets test
1 2 3 4 5 |
p = Person.new("Johnny", "Boy") p.save_to_file("./text/self.yaml") p2 = Person.load_from_file("./text/self.yaml") puts p2.first_name + " " + p2.last_name |
This one is my first ruby gem and can be found on github To use, add this line to your application’s Gemfile:
1 |
gem 'yahoo_countries_and_provinces' |
And in your code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#require the file require "yahoo_countries_and_provinces" #To get a list of the countries in english country_array = YahooCountriesAndStates::countries("en") #To get a list of the countries in swedish country_array = YahooCountriesAndStates::countries("sv") #To get Germany's provinces in english provinces_array = YahooCountriesAndStates::provinces("Germany", "en") |
Lets say that we have to write some text to a file several time in our code but we are afraid that we will try to open the file more […]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
require "securerandom" class PasswordGenerator def initialize(maxlength = 32) if maxlength > 32 raise 'The max length can not exceed 32 characters' end @maxlength = maxlength end def generate() SecureRandom.uuid.gsub('-', '')[0..@maxlength-1] end end |