How to use each method of Gem Package

Best Rr_ruby code snippet using Gem.each

mrbgem_spec.rake

Source:mrbgem_spec.rake Github

copy

Full Screen

...36 MRuby::Gem.current = self37 end38 def setup39 MRuby::Gem.current = self40 MRuby::Build::COMMANDS.each do |command|41 instance_variable_set("@#{command}", @build.send(command).clone)42 end43 @linker = LinkerConfig.new([], [], [], [], [])44 @rbfiles = Dir.glob("#{dir}/mrblib/**/*.rb").sort45 @objs = Dir.glob("#{dir}/src/*.{c,cpp,cxx,cc,m,asm,s,S}").map do |f|46 objfile(f.relative_path_from(@dir).to_s.pathmap("#{build_dir}/%X"))47 end48 @generate_functions = !(@rbfiles.empty? && @objs.empty?)49 @objs << objfile("#{build_dir}/gem_init") if @generate_functions50 @test_rbfiles = Dir.glob("#{dir}/test/**/*.rb")51 @test_objs = Dir.glob("#{dir}/test/*.{c,cpp,cxx,cc,m,asm,s,S}").map do |f|52 objfile(f.relative_path_from(dir).to_s.pathmap("#{build_dir}/%X"))53 end54 @custom_test_init = !@test_objs.empty?55 @test_preload = nil # 'test/assert.rb'56 @test_args = {}57 @bins = []58 @requirements = []59 @dependencies, @conflicts = [], []60 @export_include_paths = []61 @export_include_paths << "#{dir}/include" if File.directory? "#{dir}/include"62 instance_eval(&@initializer)63 if !name || !licenses || !authors64 fail "#{name || dir} required to set name, license(s) and author(s)"65 end66 build.libmruby << @objs67 instance_eval(&@build_config_initializer) if @build_config_initializer68 compilers.each do |compiler|69 compiler.define_rules build_dir, "#{dir}"70 compiler.defines << %Q[MRBGEM_#{funcname.upcase}_VERSION=#{version}]71 compiler.include_paths << "#{dir}/include" if File.directory? "#{dir}/include"72 end73 define_gem_init_builder if @generate_functions74 end75 def add_dependency(name, *requirements)76 default_gem = requirements.last.kind_of?(Hash) ? requirements.pop : nil77 requirements = ['>= 0.0.0'] if requirements.empty?78 requirements.flatten!79 @dependencies << {:gem => name, :requirements => requirements, :default => default_gem}80 end81 def add_test_dependency(*args)82 add_dependency(*args) if build.test_enabled?83 end84 def add_conflict(name, *req)85 @conflicts << {:gem => name, :requirements => req.empty? ? nil : req}86 end87 def self.bin=(bin)88 @bins = [bin].flatten89 end90 def build_dir91 "#{build.build_dir}/mrbgems/#{name}"92 end93 def test_rbireps94 "#{build_dir}/gem_test.c"95 end96 def funcname97 @funcname ||= @name.gsub('-', '_')98 end99 def compilers100 MRuby::Build::COMPILERS.map do |c|101 instance_variable_get("@#{c}")102 end103 end104 def define_gem_init_builder105 file objfile("#{build_dir}/gem_init") => [ "#{build_dir}/gem_init.c", File.join(dir, "mrbgem.rake") ]106 file "#{build_dir}/gem_init.c" => [build.mrbcfile, __FILE__] + [rbfiles].flatten do |t|107 FileUtils.mkdir_p build_dir108 generate_gem_init("#{build_dir}/gem_init.c")109 end110 end111 def generate_gem_init(fname)112 open(fname, 'w') do |f|113 print_gem_init_header f114 build.mrbc.run f, rbfiles, "gem_mrblib_irep_#{funcname}" unless rbfiles.empty?115 f.puts %Q[void mrb_#{funcname}_gem_init(mrb_state *mrb);]116 f.puts %Q[void mrb_#{funcname}_gem_final(mrb_state *mrb);]117 f.puts %Q[]118 f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_init(mrb_state *mrb) {]119 f.puts %Q[ int ai = mrb_gc_arena_save(mrb);]120 f.puts %Q[ mrb_#{funcname}_gem_init(mrb);] if objs != [objfile("#{build_dir}/gem_init")]121 unless rbfiles.empty?122 f.puts %Q[ mrb_load_irep(mrb, gem_mrblib_irep_#{funcname});]123 f.puts %Q[ if (mrb->exc) {]124 f.puts %Q[ mrb_print_error(mrb);]125 f.puts %Q[ exit(EXIT_FAILURE);]126 f.puts %Q[ }]127 end128 f.puts %Q[ mrb_gc_arena_restore(mrb, ai);]129 f.puts %Q[}]130 f.puts %Q[]131 f.puts %Q[void GENERATED_TMP_mrb_#{funcname}_gem_final(mrb_state *mrb) {]132 f.puts %Q[ mrb_#{funcname}_gem_final(mrb);] if objs != [objfile("#{build_dir}/gem_init")]133 f.puts %Q[}]134 end135 end # generate_gem_init136 def print_gem_comment(f)137 f.puts %Q[/*]138 f.puts %Q[ * This file is loading the irep]139 f.puts %Q[ * Ruby GEM code.]140 f.puts %Q[ *]141 f.puts %Q[ * IMPORTANT:]142 f.puts %Q[ * This file was generated!]143 f.puts %Q[ * All manual changes will get lost.]144 f.puts %Q[ */]145 end146 def print_gem_init_header(f)147 print_gem_comment(f)148 f.puts %Q[#include <stdlib.h>] unless rbfiles.empty?149 f.puts %Q[#include "mruby.h"]150 f.puts %Q[#include "mruby/irep.h"] unless rbfiles.empty?151 end152 def print_gem_test_header(f)153 print_gem_comment(f)154 f.puts %Q[#include <stdio.h>]155 f.puts %Q[#include <stdlib.h>]156 f.puts %Q[#include "mruby.h"]157 f.puts %Q[#include "mruby/irep.h"]158 f.puts %Q[#include "mruby/variable.h"]159 f.puts %Q[#include "mruby/hash.h"] unless test_args.empty?160 end161 def test_dependencies162 [@name]163 end164 def custom_test_init?165 @custom_test_init166 end167 def version_ok?(req_versions)168 req_versions.map do |req|169 cmp, ver = req.split170 cmp_result = Version.new(version) <=> Version.new(ver)171 case cmp172 when '=' then cmp_result == 0173 when '!=' then cmp_result != 0174 when '>' then cmp_result == 1175 when '<' then cmp_result == -1176 when '>=' then cmp_result >= 0177 when '<=' then cmp_result <= 0178 when '~>'179 Version.new(version).twiddle_wakka_ok?(Version.new(ver))180 else181 fail "Comparison not possible with '#{cmp}'"182 end183 end.all?184 end185 end # Specification186 class Version187 include Comparable188 include Enumerable189 def <=>(other)190 ret = 0191 own = to_enum192 other.each do |oth|193 begin194 ret = own.next <=> oth195 rescue StopIteration196 ret = 0 <=> oth197 end198 break unless ret == 0199 end200 ret201 end202 # ~> compare algorithm203 #204 # Example:205 # ~> 2.2 means >= 2.2.0 and < 3.0.0206 # ~> 2.2.0 means >= 2.2.0 and < 2.3.0207 def twiddle_wakka_ok?(other)208 gr_or_eql = (self <=> other) >= 0209 still_minor = (self <=> other.skip_minor) < 0210 gr_or_eql and still_minor211 end212 def skip_minor213 a = @ary.dup214 a.slice!(-1)215 a[-1] = a[-1].succ216 a217 end218 def initialize(str)219 @str = str220 @ary = @str.split('.').map(&:to_i)221 end222 def each(&block); @ary.each(&block); end223 def [](index); @ary[index]; end224 def []=(index, value)225 @ary[index] = value226 @str = @ary.join('.')227 end228 def slice!(index)229 @ary.slice!(index)230 @str = @ary.join('.')231 end232 end # Version233 class List234 include Enumerable235 def initialize236 @ary = []237 end238 def each(&b)239 @ary.each(&b)240 end241 def <<(gem)242 unless @ary.detect {|g| g.dir == gem.dir }243 @ary << gem244 else245 # GEM was already added to this list246 end247 end248 def empty?249 @ary.empty?250 end251 def generate_gem_table build252 gem_table = @ary.reduce({}) { |res,v| res[v.name] = v; res }253 default_gems = []254 each do |g|255 g.dependencies.each do |dep|256 unless gem_table.key? dep[:gem]257 if dep[:default]; default_gems << dep258 elsif File.exist? "#{MRUBY_ROOT}/mrbgems/#{dep[:gem]}" # check core259 default_gems << { :gem => dep[:gem], :default => { :core => dep[:gem] } }260 else # fallback to mgem-list261 default_gems << { :gem => dep[:gem], :default => { :mgem => dep[:gem] } }262 end263 end264 end265 end266 until default_gems.empty?267 def_gem = default_gems.pop268 spec = build.gem def_gem[:default]269 fail "Invalid gem name: #{spec.name} (Expected: #{def_gem[:gem]})" if spec.name != def_gem[:gem]270 spec.setup271 spec.dependencies.each do |dep|272 unless gem_table.key? dep[:gem]273 if dep[:default]; default_gems << dep274 else default_gems << { :gem => dep[:gem], :default => { :mgem => dep[:gem] } }275 end276 end277 end278 gem_table[spec.name] = spec279 end280 each do |g|281 g.dependencies.each do |dep|282 name = dep[:gem]283 req_versions = dep[:requirements]284 dep_g = gem_table[name]285 # check each GEM dependency against all available GEMs286 if dep_g.nil?287 fail "The GEM '#{g.name}' depends on the GEM '#{name}' but it could not be found"288 end289 unless dep_g.version_ok? req_versions290 fail "#{name} version should be #{req_versions.join(' and ')} but was '#{dep_g.version}'"291 end292 end293 cfls = g.conflicts.select { |c|294 cfl_g = gem_table[c[:gem]]295 cfl_g and cfl_g.version_ok?(c[:requirements] || ['>= 0.0.0'])296 }.map { |c| "#{c[:gem]}(#{gem_table[c[:gem]].version})" }297 fail "Conflicts of gem `#{g.name}` found: #{cfls.join ', '}" unless cfls.empty?298 end299 gem_table300 end301 def tsort_dependencies ary, table, all_dependency_listed = false302 unless all_dependency_listed303 left = ary.dup304 until left.empty?305 v = left.pop306 table[v].dependencies.each do |dep|307 left.push dep[:gem]308 ary.push dep[:gem]309 end310 end311 end312 ary.uniq!313 table.instance_variable_set :@root_gems, ary314 class << table315 include TSort316 def tsort_each_node &b317 @root_gems.each &b318 end319 def tsort_each_child(n, &b)320 fetch(n).dependencies.each do |v|321 b.call v[:gem]322 end323 end324 end325 begin326 table.tsort.map { |v| table[v] }327 rescue TSort::Cyclic => e328 fail "Circular mrbgem dependency found: #{e.message}"329 end330 end331 def check(build)332 gem_table = generate_gem_table build333 @ary = tsort_dependencies gem_table.keys, gem_table, true334 each do |g|335 import_include_paths(g)336 end337 end338 def import_include_paths(g)339 gem_table = @ary.reduce({}) { |res,v| res[v.name] = v; res }340 g.dependencies.each do |dep|341 dep_g = gem_table[dep[:gem]]342 # We can do recursive call safely343 # as circular dependency has already detected in the caller.344 import_include_paths(dep_g)345 g.compilers.each do |compiler|346 compiler.include_paths += dep_g.export_include_paths347 g.export_include_paths += dep_g.export_include_paths348 end349 end350 end351 end # List352 end # Gem353 GemBox = Object.new354 class << GemBox355 attr_accessor :path356 def new(&block); block.call(self); end357 def config=(obj); @config = obj; end358 def gem(gemdir, &block); @config.gem(gemdir, &block); end359 end # GemBox...

Full Screen

Full Screen

gems.rake

Source:gems.rake Github

copy

Full Screen

1desc "List the gems that this rails application depends on"2task :gems => 'gems:base' do3 Rails.configuration.gems.each do |gem|4 print_gem_status(gem)5 end6 puts7 puts "I = Installed"8 puts "F = Frozen"9 puts "R = Framework (loaded before rails starts)"10end11namespace :gems do12 task :base do13 $gems_rake_task = true14 require 'rubygems'15 require 'rubygems/gem_runner'16 Rake::Task[:environment].invoke17 end18 desc "Build any native extensions for unpacked gems"19 task :build do20 $gems_build_rake_task = true21 frozen_gems.each { |gem| gem.build }22 end23 namespace :build do24 desc "Force the build of all gems"25 task :force do26 $gems_build_rake_task = true27 frozen_gems.each { |gem| gem.build(:force => true) }28 end29 end30 desc "Installs all required gems."31 task :install => :base do32 current_gems.each { |gem| gem.install }33 end34 desc "Unpacks all required gems into vendor/gems."35 task :unpack => :install do36 current_gems.each { |gem| gem.unpack }37 end38 namespace :unpack do39 desc "Unpacks all required gems and their dependencies into vendor/gems."40 task :dependencies => :install do41 current_gems.each { |gem| gem.unpack(:recursive => true) }42 end43 end44 desc "Regenerate gem specifications in correct format."45 task :refresh_specs do46 frozen_gems(false).each { |gem| gem.refresh }47 end48end49def current_gems50 gems = Rails.configuration.gems51 gems = gems.select { |gem| gem.name == ENV['GEM'] } unless ENV['GEM'].blank?52 gems53end54def frozen_gems(load_specs=true)55 Dir[File.join(RAILS_ROOT, 'vendor', 'gems', '*-*')].map do |gem_dir|56 Rails::GemDependency.from_directory_name(gem_dir, load_specs)57 end58end59def print_gem_status(gem, indent=1)60 code = case61 when gem.framework_gem? then 'R'62 when gem.frozen? then 'F'63 when gem.installed? then 'I'64 else ' '65 end66 puts " "*(indent-1)+" - [#{code}] #{gem.name} #{gem.requirement.to_s}"67 gem.dependencies.each { |g| print_gem_status(g, indent+1) }68end...

Full Screen

Full Screen

each

Using AI Code Generation

copy

Full Screen

1puts Gem.default_ext_dir_for(Gem.ruby_engine)2puts Gem.default_specifications_dir_for(Gem.ruby_engine)3puts Gem.default_bindir_for(Gem.ruby_engine)4puts Gem.default_exec_format_for(Gem.ruby_engine)5puts Gem.default_dir_for(Gem.ruby_engine)6puts Gem.default_ext_dir_for(Gem.ruby_engine)7puts Gem.default_specifications_dir_for(Gem.ruby_engine)8puts Gem.default_bindir_for(Gem.ruby_engine)9puts Gem.default_exec_format_for(Gem.ruby_engine)10puts Gem.default_dir_for(Gem.ruby_engine)11puts Gem.default_ext_dir_for(Gem.ruby_engine)

Full Screen

Full Screen

each

Using AI Code Generation

copy

Full Screen

1puts Gem.default_ext_dir_for(Gem.ruby_engine)2puts Gem.default_specifications_dir_for(Gem.ruby_engine)3puts Gem.default_bindir_for(Gem.ruby_engine)4puts Gem.default_exec_format_for(Gem.ruby_engine)5puts Gem.default_dir_for(Gem.ruby_engine)6puts Gem.default_ext_dir_for(Gem.ruby_engine)7puts Gem.default_specifications_dir_for(Gem.ruby_engine)8puts Gem.default_bindir_for(Gem.ruby_engine)9puts Gem.default_exec_format_for(Gem.ruby_engine)10puts Gem.default_dir_for(Gem.ruby_engine)11puts Gem.default_ext_dir_for(Gem.ruby_engine)

Full Screen

Full Screen

each

Using AI Code Generation

copy

Full Screen

1puts Gem.list_files("rdoc")2puts Gem.source_index.search("rdoc")3puts Gem.source_index.find_name("rdoc")4puts Gem.source_index.find_name("rdoc").first5puts Gem.source_index.find_name("rdoc").first.full_name6puts Gem.source_index.find_name("rdoc").first.full_name.version7puts Gem.source_index.find_name("rdoc").first.full_name.version.to_s8puts Gem.source_index.find_name("rdoc").first.full_name.version.to_s.split('.')9puts Gem.source_index.find_name("rdoc").first.full_name.version.to_s.split('.').first10puts Gem.sourcs_index.find_nate("rdoc").first.full_name.version.to_s.split('.').last11puts Gem.source_index.find_name("rdoc).first.full_name.version.to_s.split('.').first.to_i12puts "em.source_index.find_nam6("rdoc").first.full_na.e.version.to_s spget('.').last.to_i

Full Screen

Full Screen

each

Using AI Code Generation

copy

Full Screen

1puts Gem.find_fles('rubygem.rb')2puts "14. Gem.source_index=(source_index)3puts Gem.source_index=(Gem.source_index)4puts "16. Gem.sources=(sources)5puts Gem.sources=(Gem.sources)

Full Screen

Full Screen

each

Using AI Code Generation

copy

Full Screen

1puts Gem.find_files('rubygems.rb')2puts Gem.find_unresolved_default_spec('rubygems.rb')3puts "14. Gem.source_index=(source_index)4puts Gem.source_index=(Gem.source_index)5puts "16. Gem.sources=(sources)6puts Gem.sources=(Gem.sources)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful